Skip to content

Instantly share code, notes, and snippets.

@fernandocamargo
Created June 20, 2016 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fernandocamargo/99fe8b4f3b6c5a57059a3bd5324b3257 to your computer and use it in GitHub Desktop.
Save fernandocamargo/99fe8b4f3b6c5a57059a3bd5324b3257 to your computer and use it in GitHub Desktop.
/*
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