Skip to content

Instantly share code, notes, and snippets.

@shai126
Created July 10, 2020 17:28
Show Gist options
  • Save shai126/cb4e5ae562baddc9645d129cd1beaefd to your computer and use it in GitHub Desktop.
Save shai126/cb4e5ae562baddc9645d129cd1beaefd to your computer and use it in GitHub Desktop.
Flexible, pure FizzBuzz
//// Implementation
function play(from, to, rules) {
const lines = [];
for (let i = from; i <= to; i++) {
const message = rules
.filter(([word, predicate]) => predicate(i))
.map(([word, predicate]) => word)
.join("");
lines.push(message || i);
}
return lines;
}
//// Usage
const lines = play(1, 100, [
["Fizz", i => i % 3 === 0],
["Buzz", i => i % 5 === 0],
]);
console.log(lines.join("\n"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment