This forum is now read-only. Please use our new forums! Go to forums
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
@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
| } |
^ - - - - - -< - - - - - - < - - - < - - - - - < - -
}
Answer 5480311c282ae3083a006004
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.
5 comments
thank you haxor makes sense to me now!
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…
@ Sogon Leong Boon Jun, except you never said something about directly leaving the IF ELSE statement ….
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.
Thanks! this helped!
Answer 54801f41282ae3083a005ce9
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.
2 comments
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”…
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
@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
}
}
1 comments
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.
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
1 comments
Can we check FizzBuzz, Fizz and Buzz using the SWITCH statement ?