Creating the "99 Bottles of Beer" Song in JavaScript

In the realm of programming, there are often playful challenges that allow developers to showcase their skills and creativity. One such task involves creating a program that outputs the lyrics to the famous English song "99 Bottles of Beer on the Wall." In this article, we'll explore two JavaScript implementations of this task, one using a simple for loop and another utilizing recursion. So, let's dive into the world of programming and have some fun with the "99 Bottles of Beer" song!

Implementing the Song with a For Loop

The first approach to creating the "99 Bottles of Beer" song in JavaScript involves using a simple for loop. Here's an example implementation:

function run(from, to) {
    for (let i = from; i > to; i--) {
        // Common part of the song
        console.log(`${i} ${i !== 1 ? 'bottles' : 'bottle'} of beer on the wall`);
        console.log(`${i} ${i !== 1 ? 'bottles' : 'bottle'} of beer!`);
        console.log('Take one down, pass it around');

        // Last string
        if (i !== 1) {
            console.log(`${i - 1} ${(i - 1) !== 1 ? 'bottles' : 'bottle'} of beer on the wall!`);
        } else {
            console.log('No more bottles of beer on the wall!');
        }
    }
}

// Run the main function with the loop
run(99, 0);

In this implementation, we use a for loop to iterate from the starting value (99) down to the ending value (0). The loop iterates for each bottle of beer, outputting the corresponding lyrics. The conditional statements ensure the correct grammar is used when referring to bottles of beer.

Implementing the Song with Recursion

The second approach to creating the "99 Bottles of Beer" song involves using recursion. Here's an example implementation:

function run(from, to) {
    let i = from;

    // Common part of the song
    console.log(`${i} ${i !== 1 ? 'bottles' : 'bottle'} of beer on the wall`);
    console.log(`${i} ${i !== 1 ? 'bottles' : 'bottle'} of beer!`);
    console.log('Take one down, pass it around');

    // Last string
    if (i !== 1) {
        console.log(`${i - 1} ${(i - 1) !== 1 ? 'bottles' : 'bottle'} of beer on the wall!`);
    } else {
        console.log('No more bottles of beer on the wall!');
    }

    if (from > to + 1) run(from - 1, to);
}

// Run the main function with recursion
run(99, 0);

In this recursive implementation, we start with the initial value (99) and output the corresponding lyrics. Then, we decrement the value and recursively call the run function until we reach the desired end value (0).

Conclusion

Creating a program that outputs the lyrics to the "99 Bottles of Beer" song in JavaScript can be a fun and engaging programming task. In this article, we explored two implementations: one using a simple for loop and another utilizing recursion. Both approaches provide an opportunity to showcase your programming skills and demonstrate different problem-solving techniques. So, why not give it a try yourself and have fun with the "99 Bottles of Beer" song in your favorite programming language? Cheers!

Subscribe to The Code Sandwiches

Sign up now to get access to the library of members-only issues.
Jamie Larson
Subscribe