Skip to content

Instantly share code, notes, and snippets.

@infernoboy
Created September 21, 2015 00:51
Show Gist options
  • Save infernoboy/ecd2711234164e0a88b3 to your computer and use it in GitHub Desktop.
Save infernoboy/ecd2711234164e0a88b3 to your computer and use it in GitHub Desktop.
Bottles of Beer
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