Last active
February 19, 2021 15:03
-
-
Save benhovinga/5b9e978479744cfdd85190efaacea7c3 to your computer and use it in GitHub Desktop.
FizzBuzz game generator
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
/** | |
* FizzBuzz game generator - If number is divisable by 3 replace with Fizz, if number is divisable by 5 replace with Buzz, if number is divisable by both 3 and 5 replace with FizzBuzz | |
* @example | |
* // returns array between 1 and 100 | |
* fizzBuzz(1, 100); | |
* @param {number} startRange | |
* @param {number} endRange | |
* @returns {array} Numbers between startRange and endRange with a number replaced by Fizz, Buzz, or FizzBuzz as required. | |
**/ | |
function fizzBuzz(startRange, endRange) { | |
let array = []; | |
for (let i = startRange; i <= endRange; i++) { | |
let message = ""; | |
if (i % 3 === 0) { | |
message += "Fizz" | |
} | |
if (i % 5 === 0) { | |
message += "Buzz" | |
} | |
if (message === "") { | |
array.push(i); | |
} | |
else { | |
array.push(message); | |
} | |
} | |
return array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment