Created
October 12, 2017 05:27
-
-
Save vpiotr/f85fcf48b85f7208dd8ed0b12074b36a to your computer and use it in GitHub Desktop.
99 Botttles of Beer in JavaScript
This file contains 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
// 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