Created
July 10, 2020 17:28
-
-
Save shai126/cb4e5ae562baddc9645d129cd1beaefd to your computer and use it in GitHub Desktop.
Flexible, pure 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
//// 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