Skip to content

Instantly share code, notes, and snippets.

@madysondesigns
Created January 10, 2012 16:59
Show Gist options
  • Save madysondesigns/1590013 to your computer and use it in GitHub Desktop.
Save madysondesigns/1590013 to your computer and use it in GitHub Desktop.
Codecademy FizzBuzz
// 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);
}
}
@madysondesigns
Copy link
Author

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