Skip to content

Instantly share code, notes, and snippets.

@Haroenv
Created April 3, 2018 07:41
Show Gist options
  • Save Haroenv/ce75c26417bd5e57e3baf56c4641b2c8 to your computer and use it in GitHub Desktop.
Save Haroenv/ce75c26417bd5e57e3baf56c4641b2c8 to your computer and use it in GitHub Desktop.
/**
* Check whether a number is a multiple of a second
*
* @param {number} num the number to check
* @param {number} divisor the number over which it's dividable
*/
const isDivisible = (num, divisor) => num % divisor === 0;
/**
* Get the value (fizz, buzz, fizzbuzz, {number}) for a specific number
*
* @param {number} i number to compute
*/
const fizzOrBuzz = i => {
if (isDivisible(i, 3) && isDivisible(i, 5)) return 'fizzbuzz';
if (isDivisible(i, 3)) return 'fizz';
if (isDivisible(i, 5)) return 'buzz';
return i;
};
/**
* Make an array with the numbers [begin, ..., end]
*
* @param begin where the range should start (inclusive)
* @param begin where the range should end (inclusive)
*/
const range = (begin, end) => new Array(end).fill().map((_, i) => i + begin);
/**
* Get a list of fizzes, buzzes and fizzbuzzes
* @param {number} length The amount of fizzes to buzz
*/
const fizzbuzz = length => range(0, length).map(i => fizzOrBuzz(i));
// eslint-disable-next-line no-console
const printArray = arr => console.log(arr.join('\n'));
printArray(fizzbuzz(16));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment