I was talking to a coworker about over engineering things, which somehow lead to me over engineering the hell out of fizzbuzz. I went for a functional recursive solution in JavaScript. What do you think?
Full disclosure: my knowledge of functional programming begins and ends at "functions are king" and "no side effects" so I would LOVE some feedback regarding functional programming. But please point out other inefficiencies as well.
fizzbuzz();
function fizzbuzz() {
var i = 1;
var result = [];
(function loop() {
var str = '';
str += fizz(i);
str += buzz(i);
result.push(ifFalsy(str, i));
if (isLt100(i++)) {
loop();
}
})();
print(result.join(', '));
}
function fizz(num) {
return isDivBy3(num) ? 'fizz' : '';
}
function buzz(num) {
return isDivBy5(num) ? 'buzz' : '';
}
function isDivBy3(num) {
return num % 3 === 0;
}
function isDivBy5(num) {
return num % 5 === 0;
}
function isLt100(num) {
return num < 100;
}
function ifFalsy(value, fallback) {
return !value ? fallback : value;
}
function print(str) {
console.log(str);
}