Solution to JavaScript’s 99 Bottles of Beer Javascript的99瓶啤酒问题

elow is my solution to printing the lyrics to 99 Bottles of Beer in JavaScript:

function beerSong() {  
var bottles;
var bottlesLeft;
for (i = 99; i >= 1; i--) {
if (i == 1) {
bottles = "bottle";
bottlesLeft = "No bottles of beer on the wall!";
} else {
bottles = "bottles";
bottlesLeft = i - 1 + " bottles of beer on the wall!";
} console.log(i+ " " + bottles + " of beer on the wall,");
console.log(i+ " " + bottles + " of beer,");
console.log("Take one down, pass it around,");
console.log(bottlesLeft);
}

}console.log(beerSong());

First, I create 2 variables: 1 for bottles and 1 for bottles left, since they will be different each time. Next I use a for loop: I set to 99 because that’s where we will begin, my stopping condition is when i reaches 1, and for each iteration I want to count down i-1. I next use an if statement for when i=1, since that will be the last verse, and for every other number of the iteration. Lastly, I use console.log to display the verses of the song.

Any thoughts or feedback? I would love to hear how others solved this problem, too!

posted @ 2023-06-26 14:35  Oops!#  阅读(11)  评论(0编辑  收藏  举报