1

Just trying to understand on why Buzz doesn't appear in the newline after Fizz for 15.

Trying to learn JavaScript from Eloquent Javascript and just got into doing the FizzBuzz exercise. Note that I've included a commented out solution where it does work (although not elegantly) but the thing I've notice that some solutions searched online show their 15 appearing with Fizz but Buzz is on a newline while my solution (which is not commented out) only shows Fizz.

Can anyone explain to me why does it do this? Just curious. The only thing I've noticed is when I use

if ((int%3 == 0) && (int%5 == 0))

either at the end or the beginning of the block is when the changes are visible.

Note:

I'm not asking for solutions. I just want an explanation to my question above. The commented solution does give me FizzBuzz for 15. Please do not misunderstand and thank you for taking your time to answer this.

My solution:

for(let int = 1; int <= 100; int++){
  if(int%3 == 0){
    console.log('Fizz');
  }
  else if(int%5 == 0){
    console.log('Buzz');
  }
  else if ((int%3 == 0) && (int%5 == 0)){
    console.log('Fizz'+'Buzz');
  }
  /*if ((int%3 == 0) && (int%5 == 0)){
    console.log('Fizz'+'Buzz');
  }
  else if(int%3 == 0){
    console.log('Fizz');
  }
  else if(int%5 == 0){
    console.log('Buzz');
  }*/

  else{    
    console.log(int);
  }
}
  • Your commented out solution is not inelegant - it's the proper way to do it. (though, you *could* store the modulo results in variables if you wanted) – CertainPerformance Jan 01 '19 at 09:55
  • 1
    `if`/`else if`/… are processed in the order you write them. In the quoted code if `int$3 == 0` is true the first branch is taken and all the others are skipped. But when `int` is 15 then `int$3 == 0` is true. You need to put the most specific cases *first*. – Richard Jan 01 '19 at 09:59
  • @CertainPerformance The storing is one of the ideas I had. Just testing out the conventional ways of doing this first. – kashfil_aziz Jan 01 '19 at 10:02
  • @Richard Oh. I didn't know if/else if/.. are processed in the order I write them. Thanks. – kashfil_aziz Jan 01 '19 at 10:12

3 Answers3

0

If you remove else from else if(int%5 == 0) you will get your desired output I guess.

Partho63
  • 3,117
  • 2
  • 21
  • 39
0

In you solution, the following block is dead code :

else if ((int%3 == 0) && (int%5 == 0)){
    console.log('Fizz'+'Buzz');

This console.log('Fizz'+'Buzz') can never be reached because ((int%3 == 0) && (int%5 == 0)) would mean that (int%3 == 0) and so the first if is executed. Because of the meaning of else if, this later code block is never reached.

So to answer directly :

show their 15 appearing with Fizz but Buzz is on a newline

This probably is a coding error as FizzBuzz usually requires writing "Fizz Buzz" on a single line for 15. I would guess they did not use any "else if" - which you did.

my solution (which is not commented out) only shows Fizz. Can anyone explain to me why does it do this.

Because else if blocks order is important, and you chose the wrong one.

Suricat
  • 131
  • 4
  • Yeah, on a new line is the wrong solution. Just wanted to know why mine doesn't show one in the new line. Thanks for letting me know that block order is important which @Richard also mentioned above in the comments. – kashfil_aziz Jan 01 '19 at 10:15
0

You should reverse the order of your if statements as you have in the commented out section. Otherwise, when int = 15, your code will match true for

if(int%3 == 0){
    console.log('Fizz');
  }

And it will never reach the other if statements.

KingV
  • 112
  • 7