Skip to content

Instantly share code, notes, and snippets.

@stahnni
Last active May 13, 2017 08:30
Show Gist options
  • Save stahnni/e59eb48878c7b9353f6a7fd83480d41d to your computer and use it in GitHub Desktop.
Save stahnni/e59eb48878c7b9353f6a7fd83480d41d to your computer and use it in GitHub Desktop.
FizzBuzz
/*Eloquent JS
FizzBuzz
Write a program that uses console.log to print all the numbers from 1
to 100, with two exceptions. For numbers divisible by 3, print "Fizz"
instead of the number, and for numbers divisible by 5 (and not 3), print
"Buzz" instead.
When you have that working, modify your program to print "FizzBuzz",
for numbers that are divisible by both 3 and 5 (and still print "Fizz" or
"Buzz" for numbers divisible by only one of those).
*/
/* for(let number = 1; number <= 100; number++){
if (number % 3 === 0){
console.log("Fizz");
}else if (number % 5 === 0 && number % 3 !== 0){
console.log("Buzz");
}
} */
/*for(let i = 1; i <= 100; i++){
let number = i + 1;
let result = "";
if (number % 3 === 0){
result = "Fizz";
}else if (number % 5 === 0){
result = "Buzz";
} else if (number % 5 === 0 && number % 3 === 0){
result= "FizzBuzz";
}
else if (!result) {
result = i+1;
}
console.log(result);
}*/
for(let number = 1; number <= 100; number++){
if (number % 3 === 0 && number % 5 === 0){
console.log("FizzBuzz");
}else if (number % 3 === 0){
console.log("Fizz");
}else if (number % 5 === 0){
result = "Buzz";
}else {console.log(number)}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment