| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | | * @param {number} initialBottlesCount |
| 8 | | * @param {number} takeDownCount |
| 9 | | * @return {string[]} |
| 7 | * Recites the lyrics to '99 Bottles of Beer on the Wall' for a range of verses. |
| 8 | * |
| 9 | * @param {number} initialBottlesCount - The starting number of bottles (0-99) |
| 10 | * @param {number} takeDownCount - The number of verses to recite (default: 1) |
| 11 | * @return {string[]} - 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 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 result = []; |
| 29 | |
| 30 | for (let i = 0; i < takeDownCount; i++) { |
| 31 | const currentBottles = initialBottlesCount - i; |
| 32 | |
| 33 | // Edge Case: Handle when currentBottles goes below 0 |
| 34 | if (currentBottles < 0) { |
| 35 | break; |
| 36 | } |
| 37 | |
| 38 | // Handle special cases for 0, 1, and 2 bottles |
| 39 | if (currentBottles === 0) { |
| 40 | result.push('No more bottles of beer on the wall, no more bottles of beer.'); |
| 41 | result.push('Go to the store and buy some more, 99 bottles of beer on the wall.'); |
| 42 | } else if (currentBottles === 1) { |
| 43 | result.push('1 bottle of beer on the wall, 1 bottle of beer.'); |
| 44 | result.push('Take it down and pass it around, no more bottles of beer on the wall.'); |
| 45 | } else if (currentBottles === 2) { |
| 46 | result.push('2 bottles of beer on the wall, 2 bottles of beer.'); |
| 47 | result.push('Take one down and pass it around, 1 bottle of beer on the wall.'); |
| 48 | } else { |
| 49 | result.push(`${currentBottles} bottles of beer on the wall, ${currentBottles} bottles of beer.`); |
| 50 | result.push(`Take one down and pass it around, ${currentBottles - 1} bottles of beer on the wall.`); |
| 51 | } |
| 52 | |
| 53 | // Add empty line between verses except for the last verse |
| 54 | if (i < takeDownCount - 1) { |
| 55 | result.push(''); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return result; |
| 13 | 60 | }; |
| 61 | |
| 62 | // Handled Edge Cases: |
| 63 | // - Invalid initialBottlesCount (not a number, negative, or > 99) |
| 64 | // - Invalid takeDownCount (not a number or negative) |
| 65 | // - takeDownCount is 0 |
| 66 | // - currentBottles goes below 0 |
| 67 | // - Special cases for 0, 1, and 2 bottles |