Skip to content

Instantly share code, notes, and snippets.

@vpiotr
Created October 12, 2017 05:27
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 vpiotr/f85fcf48b85f7208dd8ed0b12074b36a to your computer and use it in GitHub Desktop.
Save vpiotr/f85fcf48b85f7208dd8ed0b12074b36a to your computer and use it in GitHub Desktop.
99 Botttles of Beer in JavaScript
// 99 Botttles of Beer (see http://www.99-bottles-of-beer.net/)
// Language: JavaScript (SeaMonkey)
//
// Features:
// * no usage of digits
// * strings are not repeated
// * no external libs required
function bottles(x) {
return "bottle"+(((!(--x))?"":"s"));
}
function bottlesX(x, cap) {
return (x?x:(cap?"N":"n")+"o more");
}
function ofBeer(x) {
return " of beer";
}
function bottlesA(x, cap) {
return ""+bottlesX(x, cap)+" "+bottles(x)+ofBeer(x)+" on the wall";
}
function bottlesB(x) {
return ""+bottlesX(x, false)+" "+bottles(x)+ofBeer(x);
}
function bottlesLineA(c) {
return bottlesA(c, true)+", "+bottlesB(c)+".";
}
function initVal() {
return 'c'.charCodeAt('c'-'c');
}
function takeOrBuy(c) {
var take = "Take one down and pass it around";
var buy = "Go to the store and buy some more";
return (c - initVal() == 'c' - 'c'?buy:take);
}
function bottlesLineB(c) {
return takeOrBuy(c)+", "+bottlesA(c, false)+".";
}
c = initVal();
do {
print(bottlesLineA(c));
if (!c) c = initVal();
else c--;
print(bottlesLineB(c));
} while(c != initVal());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment