Created
May 6, 2015 10:47
-
-
Save Gwash3189/8afc14c7a84127b90028 to your computer and use it in GitHub Desktop.
FizzBuzz with recursion
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
function fizzBuzz(limit, logger, value){ | |
value = value || 1; | |
var logged = false; | |
if(shouldLog(mod(value, 15), logged)){ | |
logger("FizzBuzz"); | |
setLogged(); | |
} else if(shouldLog(mod(value, 5), logged)){ | |
logger("Buzz"); | |
setLogged(); | |
} else if(shouldLog(mod(value, 3), logged)) { | |
logger("Fizz"); | |
setLogged(); | |
} else { | |
logger(value); | |
setLogged(); | |
} | |
if(value !== limit){ | |
fizzBuzz.call(null, limit, logger, (value + 1)); | |
} | |
function setLogged(){ | |
logged = true; | |
} | |
function mod(value, number) { | |
return function() { | |
return value % number === 0; | |
}; | |
} | |
function shouldLog(modder, logged){ | |
return modder() && !logged; | |
} | |
} | |
fizzBuzz(100, console.log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment