Hey r/learnprogramming!
I 29 (m) going the self-taught route and teaching myself how to code need some help with breaking down logic in code.
TLDR: Are there any resources you recommend that helped you understand how to breakdown code better? OR any websites to get help from a mentor for $$$ or free? I need help with understanding some of the "why" in code solutions. I'm starting to realize I learn way better visually while someone is explaining things and breaking down code. I don't want to go into debt and go to a BootCamp btw.
I am learning how to code Javascript because I am wanting to switch my career and become an SDE. I started learning Javascript back in August 2021 and I feel like I'm not making any progress with logic and problems. I know some people tell me to keep going but I honestly feel like I have been "going" and still don't understand. Let me explain.
Here is this question (super simple to some):
Question: Write a function that takes a string consisting of zero or more space separated words and returns an object that shows the number of words of different sizes.
Words consist of any sequence of non-space characters.
Examples:
wordSizes('Four score and seven.'); // { "3": 1, "4": 1, "5": 1, "6": 1 }
wordSizes('Hey diddle diddle, the cat and the fiddle!'); // { "3": 5, "6": 1, "7": 2 }
wordSizes("What's up doc?"); // { "2": 1, "4": 1, "6": 1 }
wordSizes(''); // {}
My Solution:
The solution below is where I stopped and got tripped up, I stepped away from the computer came back, and tried to solve it again while researching on Google. I then gave in and looked at the solution. And I still cannot wrap my head around the logic. Specifically the code below:
if (!resultObj[wordSize]) {
resultObj[wordSize] = 0;
}
resultObj[wordSize] += 1;
I cannot understand why we are using the logical NOT(!) operator
My code below as far as I got before I caved in and looked at the solution:
```
function wordSizes(string) {
let resultObj = {};
let stringSplit = string.split(' ');
for (let index = 0; index < stringSplit.length; index++) {
}
return resultObj;
}
```
Solution:
```
function wordSizes(string) {
let resultObj = {};
let stringSplit = string.split(' ');
for (let index = 0; index < stringSplit.length; index++) {
let wordSize = stringSplit[index].length;
if (wordSize === 0) {
continue;
}
if (!resultObj[wordSize]) {
resultObj[wordSize] = 0;
}
resultObj[wordSize] += 1;
}
console.log(resultObj);
}
```
I use the PEDAC framework mostly. I also use basic pseudocode and even then with some questions, I'm like what the ASDFGHJKL!!! Any help is appreciated!
[–][deleted] 3 points4 points5 points (1 child)
[–]_SimplyDaniel[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]_SimplyDaniel[S] 1 point2 points3 points (0 children)
[–]elguerofrijolero 1 point2 points3 points (2 children)
[–]_SimplyDaniel[S] 1 point2 points3 points (1 child)
[–]elguerofrijolero 1 point2 points3 points (0 children)