Need A Lot of Assistance From An Actual js Dev

You shouldn’t beat yourself up about it, it is very easy to overly focus on one part and completely miss what is really happening. Taking a break to clear your head is usually really helpful in this case. All of us have spent many hours b like this in the past, personally probably amounts to hundreds of hours.

You can use the arithmetic use of += for this, n += 2 or you can write the longer form if you prefer.

I probably shouldn’t have attempted to dive into this and try explain how it works… since we are in that hole now, I’ll give it another shot, if it still doesn’t make sense then don’t worry about it too much for now.

So what I’m talking about here is that there are true and false values which are explicitly true and explicitly false. There are also other values like 1 and "hello" which are neither true or false but can be treated as if they are in some cases, this is often known as “truthyness”. Looking at the slightly clearer if based version of printing the output or n, the use of output as a condition of the if here is depending on the fact that an empty string will be treated as false and conversely a non-empty string will be treated as if it is true.

 if (output) { // <-- when the output here is empty then it behaves as if output is false, if it is not empty it behaves as though it is true
    console.log(output);
} else {
    console.log(n);
}

You can even see this in a simple comparison, "" == false will evaluate to true, "hello" == true will also evaluate to true. The === and !=== versions I mentioned before don’t do this, "hello" === true is false. The version the original author had with output || n is depending in the same way on this implicit way that some values will treated as if they are true and others will treated as if they are false.

You did remember it correctly. These comparisons have 2 versions, these (which have 3 symbols) do a strict comparison that avoids mangling the types of the values and don’t let values pretend to be true/false. They have (generally best avoided) versions with 2 symbols, == and != which are less strict and do mangle types of values.

Sorry for adding to the confusion here. So in this situation that we are discussing where there is a number that is being used in place where a boolean value is expected and it is not a strict comparison (using === / !==) then 0 will be treated as false, other numbers will be treated as true. Here are a couple of examples:

let n = 5;
if (n) {
    console.log("n is not zero");
} else {
    console.log("n is zero");
}

prints “n is not zero”

where as

let n = 0;
if (n) {
    console.log("n is not zero");
} else {
    console.log("n is zero");
}

prints “n is zero”.

The middle part is the stop condition. It should answer the question “should this loop run for the current value of the index counting variable?” with a true or false answer.

We might not be talking about the same code or it might be the way it is formatted. In that part I was meaning about the way the if constructs are used in this code at the start of the commented lines.

for (let n = 1; n <= 100; n++) { 
  let output = "";
  if (n % 3 == 0) output += "Fizz"; // if is at the start of this line
  if (n % 5 == 0) output += "Buzz"; // if is at the start of this line
  console.log(output || n); 
}  

Here is the same code formatted slightly differently that might make the if construct more obvious.

for (let n = 1; n <= 100; n++) { 
    let output = "";
    
    if (n % 3 == 0)
        output += "Fizz";

    if (n % 5 == 0)
        output += "Buzz";
    
    console.log(output || n); 
}  

The main point was that early in the reply I made some comments about how if, else and else if behave in relation to what was going wrong with your solution. I wanted to clarify that those points were not applicable to the authors solution because these are two completely separate if constructs not linked with else or else if. Does that make more sense?

Language specs generally aren’t aimed at beginners, they are mainly a technical reference for people who already have a pretty good grasp on programming and usually are needing to double check a fine detail or are trying to implement a compiler/interpreter for the language.

3 Likes