Language JavaScript
(using DOM and a closure)
| Date: | 03/07/06 |
| Author: | Christof Donat |
| URL: | http://blog.cdonat.de |
| Comments: | 0 |
| Info: | n/a |
| Score: |
function song() {
bottlesOfBeer = function(i) { return i+' bottles of beer'; }
bottlesOfBeerOnTheWall = function(i) { return this.bottlesOfBeer(i)+' on the wall'; }
takeOneDown = function() { return 'Take one down and pass it around, '; }
createVerse= function(first,second) {
var rval = document.createElement('p');
rval.appendChild(document.createTextNode(first));
rval.appendChild(document.createElement('br'));
rval.appendChild(document.createTextNode(second));
return rval;
}
getNormalVerseFunction = function(i) {
return function() {
return createVerse(
bottlesOfBeerOnTheWall(i)+', '+bottlesOfBeer(i),
takeOneDown()+bottlesOfBeerOnTheWall(i-1)+'.'
);
}
}
verse = new Array();
for( var i = 3; i < 100; i++ )
verse[i] = getNormalVerseFunction(i);
verse[2] = function() {
return createVerse(
bottlesOfBeerOnTheWall(2)+', '+bottlesOfBeer(2),
takeOneDown()+'1 bottle of beer.'
);
}
verse[1] = function() {
return createVerse(
'1 bottle of beer on the wall, 1 bottle of beer.',
takeOneDown()+bottlesOfBeerOnTheWall('no more')+'.'
);
}
verse[0] = function() {
return createVerse(
bottlesOfBeerOnTheWall('No more')+', '+bottlesOfBeer('no more'),
'Go to the store and buy some more, '+bottlesOfBeerOnTheWall(99)+'.'
);
}
this.getDom = function() {
var rval = document.createElement('div');
for( var i = 99; i >= 0 ; i-- )
rval.appendChild(verse[i]());
return rval;
}
}
document.getElementsByTagName('body')[0].appendChild(new song().getDom());
Download Source | Write Comment
Alternative Versions
| Version | Author | Date | Comments | Rate |
|---|---|---|---|---|
| Eponymous obfuscated version | JavaScribe | 01/09/09 | 4 | |
| Fully commented, OOP approach. | Ariel Flesler | 03/28/08 | 0 | |
| 3 | Brian Patrick Lee | 04/20/05 | 3 | |
| With english numbers, DOM, and callbacks | Joseph Taylor | 01/18/08 | 0 |
Download Source | Write Comment
Add Comment
Please provide a value for the fields Name,
Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.
Please don't post large portions of code here! Use the form to submit new examples or updates instead!
Comments