Skip to content

Instantly share code, notes, and snippets.

@isner
Last active September 29, 2017 03:38
Show Gist options
  • Save isner/5c9a0ecaff519518862faccbcb1d55dc to your computer and use it in GitHub Desktop.
Save isner/5c9a0ecaff519518862faccbcb1d55dc to your computer and use it in GitHub Desktop.
Beer song code kata
module.exports = function () {
this.verse = (n) => {
const otw = 'on the wall'
const numNow = n === 0 ? 'no more' : n
const itOne = numNow < 2 ? 'it' : 'one'
const numAfter = ((n - 1) < 0) ? 99 : ((n - 1) === 0) ? 'no more' : n - 1
return [
`${cap(numNow)} ${bottle(n)} ${otw}, `,
`${numNow} ${bottle(n)}.`,
`\n`,
n > 0 ? `Take ${itOne} down and pass it around, ` : `Go to the store and buy some more, `,
`${numAfter} ${bottle(numAfter)} ${otw}.`,
`\n`
].join('')
}
this.sing = (curr, end) => {
if (curr <= end) {
return new Error('invalid curr/end verse')
}
const res = []
end = end || 0
while (curr >= end) {
res.push(this.verse(curr))
curr--
}
return res.join('\n')
}
}
bottle = (n) => {
const s = n !== 1 ? 's' : ''
return `bottle${s} of beer`
}
cap = (str) => {
str = str.toString()
return str.substr(0, 1).toUpperCase() + str.substr(1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment