3/33 confused but got it right.. Really need some insight :(


#1

I'm having a lot of trouble understanding this.... so it took me like 3 1/2 hours to figure this out.. and I ultimately ended up having to look on google for some hints...

I DO NOT understand why "else if" is required as opposed to just "if".... also I HAVE NOT THE SLIGHTEST CLUE as to why I had to re position the FizzBuzz aspect.. despite the instructions listing it as Fizz, Buzz, FizzBuzz (in that order).

So... WHY in the world does the FizzBuzz equation need to be represent at the top of the list? And why does else if need to be used, why not just "if " ?!?!?!

Here is my code that worked:

var limit = 20;

for(var i = 1; i <= limit; i++ ) {
     if(i % 3 == 0 && i % 5 == 0){
        console.log("FizzBuzz"); 
    }
  else if(i % 3 == 0){
    console.log("Fizz");
}
   else if(i % 5 == 0){
        console.log("Buzz");
    }

    else {
    console.log(i);
}
};

My brain is fried after this... any insight / explanation will be of great help.


#2

What you did was first make a variable called "limit". You made that into 20, and you used it to make your variable "i" go upwards constantly until it reaches 20. Inside this "for" statement, you made an "if" statement saying that if the numbers 1 through 20 can be divisible by 3 and 5, and has no remainder. That sends the console a message "FizzBuzz".

Then you have added an "else if" statement that says that if the numbers 1 through 20 can be divisible by 3, and there is no remainder, in place comes out "Fizz".

Then you have added another "else if" statement that says that if the numbers 1 through 20 can be divisible by 5, and there is no remainder, in place comes out "Buzz".

At last, you put an "else" statement saying that if it's neither, the number just shows on the console.

To why the FizzBuzz equation is at the top of the list, you need to think of it as a square. The Buzz and Fizz equations is a rectangle. In other words, the FizzBuzz equation is a Buzz and Fizz equation together, but the Buzz equation and the Fizz equation is NOT a FizzBuzz equation.

The answer to your question is that the FizzBuzz equation is more precise, and if you put the Buzz and Fizz equations at the front of the code, then all the numbers that should be FizzBuzz would either be just Fizz or just Buzz! This is why the FizzBuzz equation is in the front.

"Else if" needs to be used because "else" indicates that it's the end of the loop, while "if" indicates the start of a loop. Think as "Else if" is always the type of loop in the middle if there's more than just an "if/else" loop.

I understand that it's pretty long, but I hope it helps you understand! :smiley:


#3

Man... THANK YOU! Truly sheds some light :smile:


#5

A simpler solution would have been to set your limit in the for statement.

for(var i = 1; i < 21; i++ )

The var limit is not needed.


#6

yeah i know, but... for the record... that helps me in no way shape or form. lol. in understanding any of the topics I mentioned.


#7

I undestand. I was just offering a simpler solution. :smile:


#8

https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Statements/if...else

if ( color === "black") {
      //IF code-block
      // is executed when color equals 'black'
}
else {
     //ELSE codeblock
     //is executed when color NOT-equal 'black'
}

With a normal IF ELSE statement
you use 1 condition-test in the IF
and if this condition renders to a Boolean true
the IF code-block is executed.

As the ELSE-statement-block does NOT take a condition
the ELSE code-block is executed only if the IF-condition rendered to Boolean false

Javascript gives you the abillity to test multiple-condition's
by giving you the opportuniy to insert an extra else if with its condition

if ( color === "black") {
      //IF code-block
      // is executed when color equals 'black'
}
else if ( color === 'green' ) {
     //ELSE-IF code-block
     //is executed when color equals 'green'
}
else {
     //ELSE codeblock
     //is executed when 
      //        color NOT-equal 'black'
     //AND
      //        color NOT-equal 'green'
}

Now your FIZZ BUZZ conditions...
-1 if i modulo 3 equals 0 then display 'FIZZ'
-2 if i modulo 5 equals 0 then display 'BUZZ'
-3 if i modulo 3 equals 0 AND i modulo 5 equals 0 display 'FIZZBUZZ'
-4 ELSE display i
thus you would have
-1 if (condition) -> code-block -> exit this IF ELSE-IF ELSE statement
-2 else if (condition) -> code-block -> exit this IF ELSE-IF ELSE statement
-3 else if (condition) -> code-block -> exit this IF ELSE-IF ELSE statement
-4 else -> code-block -> exit this IF ELSE-IF ELSE statement

The thing is that you must always have the most-specific-condition at the TOP

And something nice to know
if you have a condition i % 3 == 0 && i % 5 == 0
the same would be i%(3*5)

And as i see you are using
i % 3 == 0 in your comparison instead of i % 3 === 0
== the Book ==
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators
== discussions / opinions ==


#9

Anyone notice that the modulos where taking '==' not '==='?


#10

So you wouldn't be confused, I'll just tell you the difference between "==" and "===".

"==" Looks ONLY at the value when comparing. "===" Looks at the value AND the type.

For example:

"1" == 1 // this means true, for both the string and the number is 1

"1" === 1 // this means false, for the string 1 and the number 1 are different types

Hope it helped!:smiley: