How can I use recursion and if-else statements only to print the 99 beers lyrics?

Saurabh Staff asked 1 year ago
[showad block=1]
1 Answers
Best Answer
Saurabh Staff answered 1 year ago
Below js code snippet does it !!



(function() {
 print99BeersLyrics(99)
})();


function print99BeersLyrics (n) {
 if (n === 0) {
 n = "No more ";
 var line = n +" bottles of beer on the wall, "+n+ " bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n";
 }
 else {
 var x = (n - 1 === 1)? n-1+" bottle": (n - 1) ? n - 1 +" bottles ": "No more bottles";
 var y = (n > 1) ? n + " bottles" : n + " bottle";
 var line = y +" of beer on the wall, "+y+ " of beer.\nTake one down and pass it around, "+x +" of beer on the wall.\n";
 
 }
 console.log(line);
 if (n > 0)
 print99BeersLyrics(n-1);
 else 
 return;
}

Codepen : here