| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | | * @param {number} initialBottlesCount |
| 8 | | * @param {number} takeDownCount |
| 9 | | * @return {string[]} |
| 7 | * Recites verses of the '99 Bottles of Beer on the Wall' song. |
| 8 | * |
| 9 | * @param {number} initialBottlesCount - The starting number of bottles (0-99) |
| 10 | * @param {number} takeDownCount - How many verses to recite (default: 1) |
| 11 | * @return {string[]} - Array of verses |
| 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 invalid inputs |
| 15 | if (typeof initialBottlesCount !== 'number' || initialBottlesCount < 0 || initialBottlesCount > 99) { |
| 16 | throw new Error('Initial bottles count must be between 0 and 99'); |
| 17 | } |
| 18 | |
| 19 | if (typeof takeDownCount !== 'number' || takeDownCount < 0) { |
| 20 | throw new Error('Take down count must be a non-negative number'); |
| 21 | } |
| 22 | |
| 23 | // Edge Case: Handle when takeDownCount is 0 |
| 24 | if (takeDownCount === 0) { |
| 25 | return []; |
| 26 | } |
| 27 | |
| 28 | const verses = []; |
| 29 | |
| 30 | for (let i = 0; i < takeDownCount; i++) { |
| 31 | const currentBottles = initialBottlesCount - i; |
| 32 | |
| 33 | // Edge Case: Handle when we go below 0 bottles |
| 34 | if (currentBottles < 0) { |
| 35 | break; |
| 36 | } |
| 37 | |
| 38 | let line1, line2; |
| 39 | |
| 40 | if (currentBottles === 0) { |
| 41 | line1 = "No more bottles of beer on the wall, no more bottles of beer."; |
| 42 | line2 = "Go to the store and buy some more, 99 bottles of beer on the wall."; |
| 43 | } else if (currentBottles === 1) { |
| 44 | line1 = "1 bottle of beer on the wall, 1 bottle of beer."; |
| 45 | line2 = "Take it down and pass it around, no more bottles of beer on the wall."; |
| 46 | } else { |
| 47 | line1 = `${currentBottles} bottles of beer on the wall, ${currentBottles} bottles of beer.`; |
| 48 | const nextBottles = currentBottles - 1; |
| 49 | if (nextBottles === 1) { |
| 50 | line2 = `Take one down and pass it around, 1 bottle of beer on the wall.`; |
| 51 | } else { |
| 52 | line2 = `Take one down and pass it around, ${nextBottles} bottles of beer on the wall.`; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | verses.push(line1); |
| 57 | verses.push(line2); |
| 58 | |
| 59 | // Edge Case: Don't add empty line after the last verse |
| 60 | if (i < takeDownCount - 1) { |
| 61 | verses.push(""); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return verses; |
| 13 | 66 | }; |
| 67 | |
| 68 | // Handled Edge Cases: |
| 69 | // - Invalid initialBottlesCount (not a number or out of range 0-99) |
| 70 | // - Invalid takeDownCount (not a number or negative) |
| 71 | // - takeDownCount is 0 |
| 72 | // - Going below 0 bottles |
| 73 | // - Special case for 0 bottles |
| 74 | // - Special case for 1 bottle |
| 75 | // - Special case for 2 bottles (singular "bottle" in second line) |
| 76 | // - No empty line after the last verse |