How to resolve the algorithm 99 bottles of beer step by step in the JavaScript programming language
How to resolve the algorithm 99 bottles of beer step by step in the JavaScript programming language
Table of Contents
Problem Statement
Display the complete lyrics for the song: 99 Bottles of Beer on the Wall.
The lyrics follow this form: ... and so on, until reaching 0 (zero). Grammatical support for 1 bottle of beer is optional. As with any puzzle, try to do it in as creative/concise/comical a way as possible (simple, obvious solutions allowed, too).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm 99 bottles of beer step by step in the JavaScript programming language
The provided code is a collection of different ways to implement the lyrics of the song "99 Bottles of Beer on the Wall" in JavaScript. Here's a detailed breakdown of each approach:
-
Using a
while
loop and string concatenation:var beer = 99; while (beer > 0) { var verse = [ beer + " bottles of beer on the wall,", beer + " bottles of beer!", "Take one down, pass it around", (beer - 1) + " bottles of beer on the wall!" ].join("\n"); console.log(verse); beer--; }
This code uses a
while
loop to iterate from 99 down to 0. For each iteration, it constructs a verse of the song using string concatenation and logs it to the console. -
Using a template literal and a
while
loop:let beer = 99; while (beer > 0) { let verse = `${beer} bottles of beer on the wall, ${beer} bottles of beer! Take one down, pass it around ${beer-1} bottles of beer on the wall`; console.log(verse); beer--; }
This approach is similar to the previous one, but it uses a template literal to define the verse. Template literals provide a more concise syntax for multiline strings.
-
Using a function with a template literal and recursion:
var bottles = 99; var songTemplate = "{X} bottles of beer on the wall \n" + "{X} bottles of beer \n"+ "Take one down, pass it around \n"+ "{X-1} bottles of beer on the wall \n"; function song(x, txt) { return txt.replace(/\{X\}/gi, x).replace(/\{X-1\}/gi, x-1) + (x > 1 ? song(x-1, txt) : ""); } console.log(song(bottles, songTemplate));
This code defines a song template as a string with placeholders for the number of bottles. It then uses a recursive function called song to replace the placeholders and generate the lyrics for each verse. The result is logged to the console.
-
Using a
while
loop and document write:var beer; while ((beer = typeof beer === "undefined" ? 99 : beer) > 0) document.write( beer + " bottle" + (beer != 1 ? "s" : "") + " of beer on the wall<br>" + beer + " bottle" + (beer != 1 ? "s" : "") + " of beer<br>Take one down, pass it around<br>" + (--beer) + " bottle" + (beer != 1 ? "s" : "") + " of beer on the wall<br>" );
This code uses a
while
loop to iterate from 99 down to 0. For each iteration, it generates a verse of the song as HTML and writes it to the document using thedocument.write
function. This will display the lyrics on the web page. -
Using
Array.from
and theforEach
method:Array.from(Array(100).keys()).splice(1).reverse().forEach(n => console.log(`${n} bottle${n !== 1 ? 's' : ''} of beer on the wall\n${n} bottle${n !== 1 ? 's' : ''} of beer\nTake one down, pass it around\n${n - 1} bottle${n - 1 !== 1 ? 's' : ''} of beer on the wall\n\n`));
This code uses
Array.from
to create an array of numbers from 1 to 99. It then usessplice
,reverse
, andforEach
to iterate over the array from 99 down to 1, generating a verse of the song for each iteration. -
Using a custom
Bottles
class:function Bottles(count) { this.count = count || 99; } Bottles.prototype.take = function() { var verse = [ this.count + " bottles of beer on the wall,", this.count + " bottles of beer!", "Take one down, pass it around", (this.count - 1) + " bottles of beer on the wall!" ].join("\n"); console.log(verse); this.count--; }; Bottles.prototype.sing = function() { while (this.count) { this.take(); } }; var bar = new Bottles(99); bar.sing();
This code defines a custom
Bottles
class that represents the state of the "bottles of beer" song. The constructor function takes an optional count argument, which defaults to 99. The class has two methods:take
, which generates and logs a verse of the song while decrementing the count, andsing
, which repeatedly callstake
until the count reaches 0. -
Using a function with a template literal and document manipulation:
function bottleSong(n) { if (!isFinite(Number(n)) || n == 0) n = 100; var a = '%% bottles of beer', b = ' on the wall', c = 'Take one down, pass it around', r = '<br>' p = document.createElement('p'), s = [], re = /%%/g; while(n) { s.push((a+b+r+a+r+c+r).replace(re, n) + (a+b).replace(re, --n)); } p.innerHTML = s.join(r+r); document.body.appendChild(p); } window.onload = bottleSong;
This code defines a function called
bottleSong
that takes an optional integer argument. If the argument is not provided or is not a valid number, it defaults to 100. The function generates the lyrics of the song using a template literal and appends them to the document body as HTML. Thewindow.onload
event listener is used to call the function once the page has loaded.
Source code in the javascript programming language
var beer = 99;
while (beer > 0) {
var verse = [
beer + " bottles of beer on the wall,",
beer + " bottles of beer!",
"Take one down, pass it around",
(beer - 1) + " bottles of beer on the wall!"
].join("\n");
console.log(verse);
beer--;
}
let beer = 99;
while (beer > 0) {
let verse = `${beer} bottles of beer on the wall,
${beer} bottles of beer!
Take one down, pass it around
${beer-1} bottles of beer on the wall`;
console.log(verse);
beer--;
}
var bottles = 99;
var songTemplate = "{X} bottles of beer on the wall \n" +
"{X} bottles of beer \n"+
"Take one down, pass it around \n"+
"{X-1} bottles of beer on the wall \n";
function song(x, txt) {
return txt.replace(/\{X\}/gi, x).replace(/\{X-1\}/gi, x-1) + (x > 1 ? song(x-1, txt) : "");
}
console.log(song(bottles, songTemplate));
// Line breaks are in HTML
var beer; while ((beer = typeof beer === "undefined" ? 99 : beer) > 0) document.write( beer + " bottle" + (beer != 1 ? "s" : "") + " of beer on the wall<br>" + beer + " bottle" + (beer != 1 ? "s" : "") + " of beer<br>Take one down, pass it around<br>" + (--beer) + " bottle" + (beer != 1 ? "s" : "") + " of beer on the wall<br>" );
Array.from(Array(100).keys()).splice(1).reverse().forEach(n => console.log(`${n} bottle${n !== 1 ? 's' : ''} of beer on the wall\n${n} bottle${n !== 1 ? 's' : ''} of beer\nTake one down, pass it around\n${n - 1} bottle${n - 1 !== 1 ? 's' : ''} of beer on the wall\n\n`));
function Bottles(count) {
this.count = count || 99;
}
Bottles.prototype.take = function() {
var verse = [
this.count + " bottles of beer on the wall,",
this.count + " bottles of beer!",
"Take one down, pass it around",
(this.count - 1) + " bottles of beer on the wall!"
].join("\n");
console.log(verse);
this.count--;
};
Bottles.prototype.sing = function() {
while (this.count) {
this.take();
}
};
var bar = new Bottles(99);
bar.sing();
function bottleSong(n) {
if (!isFinite(Number(n)) || n == 0) n = 100;
var a = '%% bottles of beer',
b = ' on the wall',
c = 'Take one down, pass it around',
r = '<br>'
p = document.createElement('p'),
s = [],
re = /%%/g;
while(n) {
s.push((a+b+r+a+r+c+r).replace(re, n) + (a+b).replace(re, --n));
}
p.innerHTML = s.join(r+r);
document.body.appendChild(p);
}
window.onload = bottleSong;
You may also check:How to resolve the algorithm Align columns step by step in the Maple programming language
You may also check:How to resolve the algorithm Hello world/Graphical step by step in the Diego programming language
You may also check:How to resolve the algorithm McNuggets problem step by step in the BASIC programming language
You may also check:How to resolve the algorithm Loops/N plus one half step by step in the Ursa programming language
You may also check:How to resolve the algorithm Loops/Continue step by step in the Python programming language