Skip to content

Instantly share code, notes, and snippets.

@0xCourtney
Created March 26, 2018 15:46
Show Gist options
  • Save 0xCourtney/3ce35217da82ff288c6f9f3049a9b947 to your computer and use it in GitHub Desktop.
Save 0xCourtney/3ce35217da82ff288c6f9f3049a9b947 to your computer and use it in GitHub Desktop.
FizzBuzz - Toy Problem
// FizzBuzz
// write a function that takes in one number.
// Starting at 1, console log every number up to the number passed in.
// If the number being logged is divisible by 3 log 'Fizz' instead.
// If the number is divisible by 5 we will log 'Buzz' instead.
// If they are divisible by both 3 and 5 we will log 'FizzBuzz'
function fizzBuzz(num) {
for (let i = 1; i <= num; i++) {
if (i % 3 === 0 && i % 5 === 0) {
console.log("FizzBuzz");
} else if (i % 3 === 0) {
console.log("Fizz");
} else if (i % 5 === 0) {
console.log("Buzz");
} else {
console.log(i);
}
}
}
fizzBuzz(15);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment