Last active
September 29, 2017 03:38
-
-
Save isner/5c9a0ecaff519518862faccbcb1d55dc to your computer and use it in GitHub Desktop.
Beer song code kata
This file contains hidden or 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
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