| 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[]} - 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 invalid inputs |
| 15 | if (typeof initialBottlesCount !== 'number' || typeof takeDownCount !== 'number' || |
| 16 | initialBottlesCount < 0 || initialBottlesCount > 99 || |
| 17 | takeDownCount < 0 || takeDownCount > 100) { |
| 18 | throw new Error('Invalid input parameters'); |
| 19 | } |
| 20 | |
| 21 | const verses = []; |
| 22 | |
| 23 | // Edge Case: Handle when takeDownCount is 0 |
| 24 | if (takeDownCount === 0) { |
| 25 | return []; |
| 26 | } |
| 27 | |
| 28 | for (let i = 0; i < takeDownCount; i++) { |
| 29 | const currentBottles = initialBottlesCount - i; |
| 30 | const nextBottles = currentBottles - 1; |
| 31 | |
| 32 | // Edge Case: Handle negative bottle counts |
| 33 | if (currentBottles < 0) { |
| 34 | break; |
| 35 | } |
| 36 | |
| 37 | // Handle the special case for 0 bottles |
| 38 | if (currentBottles === 0) { |
| 39 | verses.push("No more bottles of beer on the wall, no more bottles of beer."); |
| 40 | verses.push("Go to the store and buy some more, 99 bottles of beer on the wall."); |
| 41 | } |
| 42 | // Handle the special case for 1 bottle |
| 43 | else if (currentBottles === 1) { |
| 44 | verses.push("1 bottle of beer on the wall, 1 bottle of beer."); |
| 45 | verses.push("Take it down and pass it around, no more bottles of beer on the wall."); |
| 46 | } |
| 47 | // Handle the special case for 2 bottles |
| 48 | else if (currentBottles === 2) { |
| 49 | verses.push("2 bottles of beer on the wall, 2 bottles of beer."); |
| 50 | verses.push("Take one down and pass it around, 1 bottle of beer on the wall."); |
| 51 | } |
| 52 | // Handle all other cases (3 or more bottles) |
| 53 | else { |
| 54 | verses.push(`${currentBottles} bottles of beer on the wall, ${currentBottles} bottles of beer.`); |
| 55 | verses.push(`Take one down and pass it around, ${nextBottles} bottles of beer on the wall.`); |
| 56 | } |
| 57 | |
| 58 | // Edge Case: Add empty string after each verse except the last one |
| 59 | if (i < takeDownCount - 1 && (currentBottles > 0 || (currentBottles === 0 && nextBottles === -1))) { |
| 60 | verses.push(""); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // Handled Edge Cases: Invalid inputs, takeDownCount is 0, negative bottle counts |
| 65 | return verses; |
| 13 | 66 | }; |