| 1 | | // |
| 2 | | // This is only a SKELETON file for the 'Beer Song' exercise. It's been provided as a |
| 3 | | // convenience to get you started writing code faster. |
| 4 | | // |
| 5 | | |
| 6 | | /** |
| 7 | | * @param {number} initialBottlesCount |
| 8 | | * @param {number} takeDownCount |
| 9 | | * @return {string[]} |
| 1 | /* |
| 2 | * Generates the lyrics for the beer song starting from a given number of bottles. |
| 3 | * @param {number} initialBottlesCount - The starting number of bottles of beer. |
| 4 | * @param {number} takeDownCount - The number of verses to generate (default is 1). |
| 5 | * @return {string[]} - An array of strings representing the lyrics. |
| 10 | 6 | */ |
| 11 | 7 | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 8 | // Edge Case: Handle negative initial bottle counts |
| 9 | if (initialBottlesCount < 0) { |
| 10 | throw new Error('Initial bottle count cannot be negative'); |
| 11 | } |
| 12 | |
| 13 | // Edge Case: Handle negative take down counts |
| 14 | if (takeDownCount < 0) { |
| 15 | throw new Error('Take down count cannot be negative'); |
| 16 | } |
| 17 | |
| 18 | const verses = []; |
| 19 | |
| 20 | for (let i = 0; i < takeDownCount; i++) { |
| 21 | const currentBottles = initialBottlesCount - i; |
| 22 | |
| 23 | // Edge Case: Handle when current bottles count goes below 0 |
| 24 | if (currentBottles < 0) { |
| 25 | break; |
| 26 | } |
| 27 | |
| 28 | let line1, line2; |
| 29 | |
| 30 | // Handle special cases for different bottle counts |
| 31 | if (currentBottles === 0) { |
| 32 | line1 = "No more bottles of beer on the wall, no more bottles of beer."; |
| 33 | line2 = "Go to the store and buy some more, 99 bottles of beer on the wall."; |
| 34 | } else if (currentBottles === 1) { |
| 35 | line1 = "1 bottle of beer on the wall, 1 bottle of beer."; |
| 36 | line2 = "Take it down and pass it around, no more bottles of beer on the wall."; |
| 37 | } else if (currentBottles === 2) { |
| 38 | line1 = "2 bottles of beer on the wall, 2 bottles of beer."; |
| 39 | line2 = "Take one down and pass it around, 1 bottle of beer on the wall."; |
| 40 | } else { |
| 41 | line1 = `${currentBottles} bottles of beer on the wall, ${currentBottles} bottles of beer.`; |
| 42 | line2 = `Take one down and pass it around, ${currentBottles - 1} bottles of beer on the wall.`; |
| 43 | } |
| 44 | |
| 45 | verses.push(line1); |
| 46 | verses.push(line2); |
| 47 | |
| 48 | // Add empty line between verses except for the last verse |
| 49 | if (i < takeDownCount - 1) { |
| 50 | verses.push(""); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return verses; |
| 13 | 55 | }; |
| 56 | |
| 57 | // Handled Edge Cases: negative initial bottle counts, negative take down counts, current bottles count going below 0 |