Created
September 21, 2015 00:51
-
-
Save infernoboy/ecd2711234164e0a88b3 to your computer and use it in GitHub Desktop.
Bottles of Beer
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
var playBottleSong = function (numberOfBottles) { | |
var hasBottlesTemplate = '{beforeTakenDown} {bottlesBefore} of beer on the wall, ' + | |
'{beforeTakenDown} {bottlesBefore} of beer. Take one down and pass it around, ' + | |
'{afterTakenDown} {bottlesAfter} of beer on the wall.<br>'; | |
var noBottlesTemplate = 'No bottles of beer on the wall, no bottles of beer. ' + | |
'Go to the store and buy some more, 99 bottles of beer on the wall.'; | |
var bottleInfo = { | |
beforeTakenDown: numberOfBottles, | |
afterTakenDown: numberOfBottles - 1, | |
bottlesBefore: 'bottle' + (numberOfBottles == 1 ? '' : 's'), | |
bottlesAfter: 'bottle' + (numberOfBottles == 2 ? '' : 's' | |
}; | |
if (numberOfBottles > 0) { | |
var songLine = hasBottlesTemplate; | |
for (var key in bottleInfo) | |
songLine = songLine.replace(new RegExp('{' + key + '}', 'g'), bottleInfo[key]); | |
} else { | |
var songLine = noBottlesTemplate; | |
} | |
return numberOfBottles > 0 ? songLine + playBottleSong(numberOfBottles - 1) : songLine; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment