| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | | * @param {number} initialBottlesCount |
| 8 | | * @param {number} takeDownCount |
| 9 | | * @return {string[]} |
| 7 | * Generates the lyrics for the beer song starting from a given number of bottles |
| 8 | * @param {number} initialBottlesCount - The initial number of bottles (0-99) |
| 9 | * @param {number} takeDownCount - How many verses to generate (default: 1) |
| 10 | * @return {string[]} - Array of verses for the beer song |
| 10 | 11 | */ |
| 11 | 12 | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 13 | // Edge Case: Handle negative initial bottle counts |
| 14 | if (initialBottlesCount < 0) { |
| 15 | initialBottlesCount = 0; |
| 16 | } |
| 17 | |
| 18 | // Edge Case: Handle negative take down counts |
| 19 | if (takeDownCount < 0) { |
| 20 | takeDownCount = 0; |
| 21 | } |
| 22 | |
| 23 | const verses = []; |
| 24 | |
| 25 | for (let i = 0; i < takeDownCount; i++) { |
| 26 | const currentBottles = initialBottlesCount - i; |
| 27 | |
| 28 | // Edge Case: Handle when we go below 0 bottles |
| 29 | if (currentBottles < 0) { |
| 30 | break; |
| 31 | } |
| 32 | |
| 33 | if (currentBottles === 0) { |
| 34 | verses.push( |
| 35 | "No more bottles of beer on the wall, no more bottles of beer.", |
| 36 | "Go to the store and buy some more, 99 bottles of beer on the wall." |
| 37 | ); |
| 38 | } else if (currentBottles === 1) { |
| 39 | verses.push( |
| 40 | "1 bottle of beer on the wall, 1 bottle of beer.", |
| 41 | "Take it down and pass it around, no more bottles of beer on the wall." |
| 42 | ); |
| 43 | } else if (currentBottles === 2) { |
| 44 | verses.push( |
| 45 | "2 bottles of beer on the wall, 2 bottles of beer.", |
| 46 | "Take one down and pass it around, 1 bottle of beer on the wall." |
| 47 | ); |
| 48 | } else { |
| 49 | verses.push( |
| 50 | `${currentBottles} bottles of beer on the wall, ${currentBottles} bottles of beer.`, |
| 51 | `Take one down and pass it around, ${currentBottles - 1} bottles of beer on the wall.` |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | // Edge Case: Add empty line between verses except for the last verse |
| 56 | if (i < takeDownCount - 1) { |
| 57 | verses.push(""); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return verses; |
| 13 | 62 | }; |
| 63 | |
| 64 | // Handled Edge Cases: negative initial bottle counts, negative take down counts, going below 0 bottles, special wording for 0, 1, and 2 bottles |