Skip to content

Instantly share code, notes, and snippets.

@ajchambeaud
Created December 2, 2017 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajchambeaud/6427a270cb4ef6f5d505e717c48a3a23 to your computer and use it in GitHub Desktop.
Save ajchambeaud/6427a270cb4ef6f5d505e717c48a3a23 to your computer and use it in GitHub Desktop.
JS FizzBuzz
/*
* Write a program that prints the numbers from 1 to 100.
* But for multiples of three print “Fizz” instead of the number
* and for the multiples of five print “Buzz”.
* For numbers which are multiples of both three and five print “FizzBuzz”
*/
const range = (init, end) => {
const nums = [];
for(let i = init; i <= end; i ++) {
nums.push(i);
}
return nums;
};
const multipleOf = (num, x) => x % num === 0;
const printNumber = num => {
if(multipleOf(3, num) && multipleOf(5, num))
return console.log("FizzBuzz");
if(multipleOf(5, num))
return console.log("Buzz");
if(multipleOf(3, num))
return console.log("Fizz");
console.log(num);
}
const nums = range(1, 100);
nums.forEach(n => printNumber(n));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment