Last active
September 8, 2018 06:11
-
-
Save mfalade/fc2787257878a05afecdc876250a6cdf to your computer and use it in GitHub Desktop.
Foo Bar: A playful attempt to implement the common foobar problem, using HOFs; without having to do an explicit check for the value `15`, nor `n % 3 === 0 && n % 5 === 0`
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 getLabelForValue(n) { | |
return function(divisor) { | |
var labelMapping = { | |
3: 'foo', | |
5: 'bar' | |
}; | |
if (n && n % divisor === 0 ) { | |
return labelMapping[divisor]; | |
} | |
return null; | |
} | |
} | |
function fooBar(n) { | |
for (let i = 1, max = n + 1; i < max; i++) { | |
var labelForMultiplesOf3 = getLabelForValue(i)(3) || ''; | |
var labelForMultiplesOf5 = getLabelForValue(i)(5) || ''; | |
var res = `${labelForMultiplesOf3}${labelForMultiplesOf5}` || i; | |
console.log(res); | |
} | |
} | |
fooBar(50); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment