This forum is now read-only. Please use our new forums! Go to forums

banner
Close banner
0 points
Submitted by Ali Marwan
over 9 years

3/33 Need an answer to this if/else order!

Why if putting the (i%3===0 && i%5===0) as the first IF statement the program will run counting while if putting it at the end before printing the number it doesn’t work! here is the example for the working and non-working code! please assist me with a clear explanation!

THE WORKING CODE

for(i=1;i<21;i++){ if (i%3===0 && i%5===0){ <<========== PLACED @ THE BEGINNING console.log(“FizzBuzz”); } else { if (i%3===0){ console.log(“Fizz”); } else { if (i%5===0){ console.log(“Buzz”); } else { console.log(i); } } } }

THE NON-WORKING CODE

for(i=1;i<21;i++){ if (i%3===0){ console.log(“Fizz”); } else { if (i%5===0){ console.log(“Buzz”); } else { if (i%3===0 && i%5===0){ <<========== PLACED @ THE END console.log(“FizzBuzz”); } else { console.log(i); } } } }

Answer 548059268c1ccc2c9e0074bb

1 vote

Permalink

@Ali Marwan Maybe in a Diagram….

program
flow
  |
  V                                                - - - -
for(i=1;i<21;i++){ - - - - - i >= 21 - - - - - -> | EXIT |
|      |                                           - - - -
^      V
|   (i < 21)
^      V
|     if (i%3===0){
|          console.log("Fizz"); - - - - - > - - - -| 
|     }                                            |
^     else {                                       V
|          if (i%5===0){                           |
|              console.log("Buzz"); - - - > - - - -V
|          }                                       |
^          else {                                  V
|             if (i%3===0 && i%5===0) {            |
|                     console.log("FizzBuzz"); - > |
|             }                                    |
|             else {                               V
|                 console.log(i); - - - - - > - - -|
|             }                                    |
^          }                                       V
|      }                                           |
^ - - - - - -< - - - - - - < - - - < - - - - - < - -
 }                                            
points
Submitted by Leon
over 9 years

1 comments

Ali Marwan over 9 years

Can we check FizzBuzz, Fizz and Buzz using the SWITCH statement ?

Answer 5480311c282ae3083a006004

0 votes

Permalink

Well a condition executes the if case if the condition is true and the else case otherwise.

(This is just your code formatted by jsbeautifier.org and used the code feature {}-button)

for (i = 1; i < 21; i++) {
    if (i % 3 === 0) {
        console.log("Fizz");
    } else {
        if (i % 5 === 0) {
            console.log("Buzz");
        } else {
            if (i % 3 === 0 && i % 5 === 0) { <<= === === === PLACED@ THE END
                console.log("FizzBuzz");
            } else {
                console.log(i);
            }
        }
    }
}

So if i % 3 === 0 && i % 5 === 0 is true its pretty obvious that also i % 3 === 0 is true as it is an important part of it. So as i % 3 === 0 is true, you print Fizz the else case is skipped and you go on with the next number.

Also you could use else if{} instead of else { if} it does basically the same, you just need fewer levels of indentation.

points
Submitted by haxor789
over 9 years

5 comments

Ali Marwan over 9 years

thank you haxor makes sense to me now!

Sogon Leong Boon Jun over 9 years

LOL! I explained the same thing except not in details. Since i % 3 === 0 is true, it will never reach the next else if case, whereby i % 3 === 0 && i % 5 === 0 is…

Leon over 9 years

@ Sogon Leong Boon Jun, except you never said something about directly leaving the IF ELSE statement ….

Sogon Leong Boon Jun over 9 years

I did, (Once it meets that condition, it will continue to the next number.) meaning it will not check the “else if” or “else” that comes next.

Kirill almost 9 years

Thanks! this helped!

Answer 54801f41282ae3083a005ce9

-1 votes

Permalink

To my understanding, the code reads from top that if it meets that condition, it will execute that block of code but if it doesn’t, it moves down to the next which is the else if / else. Once it meets that condition, it will continue to the next number. Hope this is clear for you.

points
over 9 years

2 comments

Ali Marwan over 9 years

I know that order from top to bottom but since we are looping the number from 1 to 20 and letting it detects the Fizz and Buzz wherever found so its not necessarily listing the conditions in order “that’s to my understanding in this context”…

Ali Marwan over 9 years

and since you said it reads from top to bottom why the second “non-wokring code” i listed showing error ? so it should just OKs it and read from top Fizz then Buzz then FizzBuzz!

Answer 5481b99f9376765a48000be7

-1 votes

Permalink

@Ali Marwan,

fizzbuzz with a switch-statement
for(i=1;i<21;i++){
  switch( true ) {
    
    case (i%3===0 && i%5===0):
         console.log("FizzBuzz");
         break;   //<== exit from the swtich, return into for-loop
    case (i%3===0):
         console.log("Fizz");
         break;   //<== exit from the swtich, return into for-loop
    case (i%5===0):
         console.log("Buzz");
         break;   //<== exit from the swtich, return into for-loop
    default:
         console.log(i);
         //automatic    exit from the swtich, return into for-loop
  }
}
points
Submitted by Leon
over 9 years

1 comments

Ali Marwan over 9 years

thank you so much leon just i’m trying to think out of the box! thanks for the help and thanks to Sogon leon and Haxor too.