| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | | * @param {number} initialBottlesCount |
| 8 | | * @param {number} takeDownCount |
| 9 | | * @return {string[]} |
| 7 | * Generates verses of the "99 Bottles of Beer" song. |
| 8 | * |
| 9 | * @param {number} initialBottlesCount - The starting number of bottles (0-99) |
| 10 | * @param {number} takeDownCount - How many verses to generate (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 | if (currentBottles === 0) { |
| 39 | verses.push( |
| 40 | 'No more bottles of beer on the wall, no more bottles of beer.', |
| 41 | 'Go to the store and buy some more, 99 bottles of beer on the wall.' |
| 42 | ); |
| 43 | } else if (currentBottles === 1) { |
| 44 | verses.push( |
| 45 | '1 bottle of beer on the wall, 1 bottle of beer.', |
| 46 | 'Take it down and pass it around, no more bottles of beer on the wall.' |
| 47 | ); |
| 48 | } else if (currentBottles === 2) { |
| 49 | verses.push( |
| 50 | '2 bottles of beer on the wall, 2 bottles of beer.', |
| 51 | 'Take one down and pass it around, 1 bottle of beer on the wall.' |
| 52 | ); |
| 53 | } else { |
| 54 | verses.push( |
| 55 | `${currentBottles} bottles of beer on the wall, ${currentBottles} bottles of beer.`, |
| 56 | `Take one down and pass it around, ${currentBottles - 1} bottles of beer on the wall.` |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | // Edge Case: Add empty string after each verse except the last one |
| 61 | if (i < takeDownCount - 1) { |
| 62 | verses.push(''); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return verses; |
| 13 | 67 | }; |
| 68 | |
| 69 | // Handled Edge Cases: |
| 70 | // 1. Invalid initialBottlesCount (not a number or out of range 0-99) |
| 71 | // 2. Invalid takeDownCount (not a number or negative) |
| 72 | // 3. takeDownCount is 0 |
| 73 | // 4. Going below 0 bottles |
| 74 | // 5. Special case for 0 bottles |
| 75 | // 6. Special case for 1 bottle |
| 76 | // 7. Special case for 2 bottles |
| 77 | // 8. Adding empty string separators between verses |