Created
June 20, 2016 15:48
-
-
Save fernandocamargo/99fe8b4f3b6c5a57059a3bd5324b3257 to your computer and use it in GitHub Desktop.
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
/* | |
Write a program in JavaScript that prints the numbers from 1 to 100. | |
But for multiples of seven print "Sou" instead of the number and for | |
the multiples of eleven print "Dev". For numbers which are multiples | |
of both seven and eleven print "SouDev". | |
(note: printing with console.log() is fine) | |
*/ | |
console.clear() | |
const options = { | |
counter: 1, | |
limit: 100, | |
translation: { | |
7: 'Sou', | |
11: 'Dev' | |
} | |
} | |
const print = (value) => console.log(value) | |
const multiplier = (value, comparable) => ((parseInt(value, 10) % parseInt(comparable, 10)) === 0) | |
const concat = function (stack, key) { | |
return (multiplier(this.value, key) ? stack.concat(options.translation[key]) : stack) | |
} | |
const match = (value) => Object.keys(options.translation).reduce(concat.bind({value}), []) | |
const translate = (value) => { | |
const matches = match(value) | |
return (matches.length ? matches.join('') : value) | |
} | |
const populate = () => translate(options.counter++) | |
const range = () => Array(options.limit).fill().map(populate) | |
const init = () => range().forEach(print) | |
init() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment