| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | | * @param {number} initialBottlesCount |
| 8 | | * @param {number} takeDownCount |
| 9 | | * @return {string[]} |
| 7 | * Generates the lyrics for a range of verses in 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 strings representing the song 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 negative initial bottles count |
| 15 | if (initialBottlesCount < 0) { |
| 16 | throw new Error('Initial bottles count cannot be negative'); |
| 17 | } |
| 18 | |
| 19 | // Edge Case: Handle when takeDownCount is 0 or negative |
| 20 | if (takeDownCount <= 0) { |
| 21 | return []; |
| 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 currentBottles goes below 0 |
| 30 | if (currentBottles < 0) { |
| 31 | break; |
| 32 | } |
| 33 | |
| 34 | // Handle special cases for different bottle counts |
| 35 | if (currentBottles === 0) { |
| 36 | verses.push( |
| 37 | 'No more bottles of beer on the wall, no more bottles of beer.', |
| 38 | 'Go to the store and buy some more, 99 bottles of beer on the wall.' |
| 39 | ); |
| 40 | } else if (currentBottles === 1) { |
| 41 | verses.push( |
| 42 | '1 bottle of beer on the wall, 1 bottle of beer.', |
| 43 | 'Take it down and pass it around, no more bottles of beer on the wall.' |
| 44 | ); |
| 45 | } else if (currentBottles === 2) { |
| 46 | verses.push( |
| 47 | '2 bottles of beer on the wall, 2 bottles of beer.', |
| 48 | 'Take one down and pass it around, 1 bottle of beer on the wall.' |
| 49 | ); |
| 50 | } else { |
| 51 | verses.push( |
| 52 | `${currentBottles} bottles of beer on the wall, ${currentBottles} bottles of beer.`, |
| 53 | `Take one down and pass it around, ${currentBottles - 1} bottles of beer on the wall.` |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | // Add empty string for spacing between verses (except after the last verse) |
| 58 | if (i < takeDownCount - 1) { |
| 59 | verses.push(''); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return verses; |
| 13 | 64 | }; |
| 65 | |
| 66 | // Handled Edge Cases: negative initial bottles count, zero or negative takeDownCount, currentBottles going below 0 |