Skip to content

Instantly share code, notes, and snippets.

@GreggSetzer
Created January 30, 2018 18:04
Show Gist options
  • Save GreggSetzer/935f344b5606e46804ada2c468067b79 to your computer and use it in GitHub Desktop.
Save GreggSetzer/935f344b5606e46804ada2c468067b79 to your computer and use it in GitHub Desktop.
Javascript Interview Question: Fizz Buzz
/*
Create a function that console logs the numbers from 1 to n.
- Print "fizz" for multiples of three instead of the number.
- Print "buzz" for multiples of five instead of the number.
- Print "fizzbuzz" for numbers that are multiples of three and five instead of the number.
- Otherwise, print the number itself.
*/
function fizzBuzz(num) {
for (let i = 1; i <= num; i++) {
const mod3 = i % 3 === 0;
const mod5 = i % 5 === 0;
if (mod3 && mod5) {
console.log('fizzbuzz');
} else if (mod5) {
console.log('buzz');
} else if (mod3) {
console.log('fizz');
} else {
console.log(i);
}
}
}
//Try it
fizzBuzz(16);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment