Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

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: (3.00 in 58 votes)
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

VersionAuthorDateCommentsRate
Eponymous obfuscated versionJavaScribe01/09/094
Fully commented, OOP approach.Ariel Flesler03/28/080
3Brian Patrick Lee04/20/053
With english numbers, DOM, and callbacksJoseph Taylor01/18/080

Comments

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!

Name:

eMail:

URL:

Security Code:
  
Comment: