Skip to content

Instantly share code, notes, and snippets.

@mfalade
Last active September 8, 2018 06:11
Show Gist options
  • Save mfalade/fc2787257878a05afecdc876250a6cdf to your computer and use it in GitHub Desktop.
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`
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