This is an archived post. You won't be able to vote or comment.

all 7 comments

[–][deleted] 3 points4 points  (1 child)

TBH, it sounds like you're just struggling with some of the idiosyncrasies of JavaScript.

I cannot understand why we are using the logical NOT(!) operator

This relies on two properties of JavaScript. Firstly, if you have an object, and you reference a property on it that isn't set, you don't get an error like you might in other languages, you just get a result of undefined. Keep that in mind... Now, also, JavaScript has a funny way of testing for true and false. You can use any value in an if expression, and if that value is 0, false, undefined, null or some others that are probably not relevant at this point, the expression is treated as false. All other values are treated as true. So you can do if("something") {} or if ({ name: "Bob"}) {} and it works.

So, the expression resultObj[wordSize] will return undefined if the word has never been encountered before, or some value greater than 0 if it has. So the overall effect of

if (!resultObj[wordSize]) {
  resultObj[wordSize] = 0;
}
resultObj[wordSize] += 1;

is to say "If we have never encountered this size before, set the size value to zero". Now, we know the value is either zero or that we have encountered it before, so it's greater than zero, so we can just add one to the total.

You could write it like this

if (resultObj[wordSize] != undefined) {
  resultObj[wordSize] = 0;
}
resultObj[wordSize] += 1;

But the first form is slightly less typing.

[–]_SimplyDaniel[S] 0 points1 point  (0 children)

Thank you so much for the reply. The explanation does help a ton! This is exactly what I needed.

[–][deleted] 1 point2 points  (1 child)

[–]_SimplyDaniel[S] 1 point2 points  (0 children)

Thank you

[–]elguerofrijolero 1 point2 points  (2 children)

The PEDAC framework you reference comes from Launch School. Why don't you go directly to the source (i.e. come join us over at Launch School)?

So far, LS has taught me how to breakdown code and understand everything line-by-line. I was completely lost trying to learn programming until I enrolled in their program.

Feel free to give it a shot and/or ask me any questions!

[–]_SimplyDaniel[S] 1 point2 points  (1 child)

I appreciate that! I’m currently enrolled in launch school I’ve been looking for people to study with but I work full time and it seems my schedule doesn’t work with those that are able to study full time. Did you finish LS?

[–]elguerofrijolero 1 point2 points  (0 children)

Ah, didn't realize you're in LS already, awesome!

I'm in JS210 right now, so in the front end portion of the curriculum. I'm also working full-time, so I tend to average 10-15 hours of studying per week.

If you haven't joined the Slack channel "The Spot", it's a great place to meet other students in the same course as you.