Skip to content

Instantly share code, notes, and snippets.

@qb20nh
Created September 27, 2021 14:50
Show Gist options
  • Save qb20nh/93c7f0195d2c108d47f5a553d59a834f to your computer and use it in GitHub Desktop.
Save qb20nh/93c7f0195d2c108d47f5a553d59a834f to your computer and use it in GitHub Desktop.
FizzBuzz
const testFizzBuzz = (fizzbuzz) => {
const assert = (value) => {
if (value === true) {
return;
} else {
throw 'Assertion failed';
}
};
const input = [ ...Array(15).keys() ].map(e=>e+1);
const output = [
1,
2,
'Fizz',
4,
'Buzz',
'Fizz',
7,
8,
'Fizz',
'Buzz',
11,
'Fizz',
13,
14,
'FizzBuzz'
];
input.forEach((v, i) => {
assert(fizzbuzz(input[i]) == output[i]);
});
console.log('Your fizzbuzz is good to go');
}
const FizzBuzz = (num) => {
let string = '';
if (num % 3 == 0) {
string = string + 'Fizz';
}
if (num % 5 == 0) {
string = string + 'Buzz';
}
if (string != '') {
return string;
}
else{
return num;
}
}
const GeneralizedFizzBuzzMaker = ([...multiples], [...tokens]) => (num) => {
let string = '';
multiples.forEach((v, i) => {
if (num % v == 0) {
string = string + tokens[i];
}
});
if (string != '') {
return string;
}
else{
return num;
}
}
testFizzBuzz(FizzBuzz);
const defaultFizzBuzz = GeneralizedFizzBuzzMaker([3,5],['Fizz','Buzz']);
testFizzBuzz(defaultFizzBuzz);
const superbFizzBuzz = GeneralizedFizzBuzzMaker([6,10,16,25], ['Fang', 'Bunk', 'Hello', 'World']);
for(let i = 1; i <= 100; i++) {
console.log('superbFizzBuzz', i, superbFizzBuzz(i));
}
const ultraFizzBuzz = GeneralizedFizzBuzzMaker([2,3,5,7,11,13], ['soso','good','better','great','supergreat','fuckinggood']);
for(let i = 1; i <= 100; i++) {
console.log('ultraFizzBuzz', i, ultraFizzBuzz(i));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment