| 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 specific number of bottles. |
| 8 | * |
| 9 | * @param {number} initialBottlesCount - The initial number of bottles to start reciting from |
| 10 | * @param {number} takeDownCount - The number of verses to recite (default: 1) |
| 11 | * @return {string[]} - An array of strings representing the lyrics |
| 10 | 12 | */ |
| 11 | 13 | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 14 | // Edge Case: Handle negative initial bottle counts |
| 15 | if (initialBottlesCount < 0) { |
| 16 | throw new Error('Initial bottle count cannot be negative'); |
| 17 | } |
| 18 | |
| 19 | // Edge Case: Handle negative take down counts |
| 20 | if (takeDownCount < 0) { |
| 21 | throw new Error('Take down count cannot be negative'); |
| 22 | } |
| 23 | |
| 24 | const verses = []; |
| 25 | |
| 26 | for (let i = 0; i < takeDownCount; i++) { |
| 27 | const currentBottles = initialBottlesCount - i; |
| 28 | |
| 29 | // Edge Case: Handle when we've run out of bottles |
| 30 | if (currentBottles < 0) { |
| 31 | break; |
| 32 | } |
| 33 | |
| 34 | let line1, line2; |
| 35 | |
| 36 | if (currentBottles === 0) { |
| 37 | line1 = "No more bottles of beer on the wall, no more bottles of beer."; |
| 38 | line2 = "Go to the store and buy some more, 99 bottles of beer on the wall."; |
| 39 | } else { |
| 40 | // Handle singular vs plural for bottles |
| 41 | const bottleWord = currentBottles === 1 ? 'bottle' : 'bottles'; |
| 42 | line1 = `${currentBottles} ${bottleWord} of beer on the wall, ${currentBottles} ${bottleWord} of beer.`; |
| 43 | |
| 44 | const nextBottles = currentBottles - 1; |
| 45 | |
| 46 | if (nextBottles === 0) { |
| 47 | line2 = "Take it down and pass it around, no more bottles of beer on the wall."; |
| 48 | } else if (nextBottles === 1) { |
| 49 | line2 = "Take one down and pass it around, 1 bottle of beer on the wall."; |
| 50 | } else { |
| 51 | line2 = `Take one down and pass it around, ${nextBottles} bottles of beer on the wall.`; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | verses.push(line1); |
| 56 | verses.push(line2); |
| 57 | |
| 58 | // Add empty line between verses except for the last verse |
| 59 | if (i < takeDownCount - 1) { |
| 60 | verses.push(""); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return verses; |
| 13 | 65 | }; |
| 66 | |
| 67 | // Handled Edge Cases: negative initial bottle counts, negative take down counts, running out of bottles, singular vs plural handling, last verse formatting |