Created
December 2, 2017 18:05
-
-
Save ajchambeaud/6427a270cb4ef6f5d505e717c48a3a23 to your computer and use it in GitHub Desktop.
JS FizzBuzz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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