Created
January 10, 2012 16:59
-
-
Save madysondesigns/1590013 to your computer and use it in GitHub Desktop.
Codecademy FizzBuzz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add an else statement in case the number is divisible by 5. | |
// for the numbers 1 through 20, | |
for (i=1; i<=20; i++) { | |
// if the number is divisible by 3, write "Fizz" | |
if ( i % 3 === 0 ) { | |
// if the number is divisible by 3 and 5, write "FizzBuzz" | |
if ( i % 5 === 0 ) { | |
console.log("FizzBuzz"); | |
} | |
else { | |
console.log("Fizz"); | |
} | |
} | |
// if the number is divisible by 5, write "Buzz" | |
else if ( i % 5 === 0 ) { | |
console.log("Buzz"); | |
} | |
// otherwise, write just the number | |
else { | |
console.log(i); | |
} | |
} |
Aha, that was it! I didn't know that you could just continue working with the result of the i % 3 test. I thought you had to find a way to say if i % 3 === 0 AND i % 5 === 0.
Thank you so much!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You've got a logic/structure problem here.
If the number is divisible by three, then the test at line 7 is true and line 8 executes.
If the number is divisible by five, then the test at line 12 is true and line 13 executes.
That's all good.
But the test at line 18 will never be true because anything that's divisible by 3 was already handled at line 7. So everything from 18 to 26 will never be executed.
You need to think about modifying line 8 into a block that further tests what's going on with numbers that are divisible by three. "If this number (that I already know is divisible by three) is divisible by five, print FizzBuzz, otherwise print Fizz".