| 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('initialBottlesCount must be between 0 and 99'); |
| 17 | } |
| 18 | |
| 19 | if (typeof takeDownCount !== 'number' || takeDownCount < 0) { |
| 20 | throw new Error('takeDownCount must be a non-negative number'); |
| 21 | } |
| 22 | |
| 23 | const verses = []; |
| 24 | |
| 25 | // Edge Case: Handle when takeDownCount is 0 |
| 26 | if (takeDownCount === 0) { |
| 27 | return []; |
| 28 | } |
| 29 | |
| 30 | for (let i = 0; i < takeDownCount; i++) { |
| 31 | const currentBottles = initialBottlesCount - i; |
| 32 | const nextBottles = currentBottles - 1; |
| 33 | |
| 34 | // Edge Case: Handle when currentBottles is less than 0 |
| 35 | if (currentBottles < 0) { |
| 36 | break; |
| 37 | } |
| 38 | |
| 39 | let currentBottleText = ''; |
| 40 | let nextBottleText = ''; |
| 41 | let actionText = ''; |
| 42 | |
| 43 | // Handle special cases for bottle text |
| 44 | if (currentBottles === 0) { |
| 45 | currentBottleText = 'No more bottles'; |
| 46 | } else if (currentBottles === 1) { |
| 47 | currentBottleText = '1 bottle'; |
| 48 | } else { |
| 49 | currentBottleText = `${currentBottles} bottles`; |
| 50 | } |
| 51 | |
| 52 | if (nextBottles === 0) { |
| 53 | nextBottleText = 'no more bottles'; |
| 54 | } else if (nextBottles === 1) { |
| 55 | nextBottleText = '1 bottle'; |
| 56 | } else if (nextBottles < 0) { |
| 57 | // Edge Case: Handle when we've run out of bottles |
| 58 | nextBottleText = '99 bottles'; |
| 59 | } else { |
| 60 | nextBottleText = `${nextBottles} bottles`; |
| 61 | } |
| 62 | |
| 63 | // Handle special cases for action text |
| 64 | if (currentBottles === 1) { |
| 65 | actionText = 'Take it down and pass it around'; |
| 66 | } else if (currentBottles === 0) { |
| 67 | actionText = 'Go to the store and buy some more'; |
| 68 | } else { |
| 69 | actionText = 'Take one down and pass it around'; |
| 70 | } |
| 71 | |
| 72 | // Build the verse |
| 73 | verses.push(`${currentBottleText} of beer on the wall, ${currentBottleText.toLowerCase()} of beer.`); |
| 74 | |
| 75 | // Edge Case: Handle the final verse when we run out of bottles |
| 76 | if (currentBottles === 0) { |
| 77 | verses.push(`${actionText}, ${nextBottleText} of beer on the wall.`); |
| 78 | } else { |
| 79 | verses.push(`${actionText}, ${nextBottleText} of beer on the wall.`); |
| 80 | } |
| 81 | |
| 82 | // Add empty line between verses except for the last verse |
| 83 | if (i < takeDownCount - 1) { |
| 84 | verses.push(''); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return verses; |
| 13 | 89 | }; |
| 90 | |
| 91 | // Handled Edge Cases: |
| 92 | // - Invalid initialBottlesCount (< 0 or > 99) |
| 93 | // - Invalid takeDownCount (< 0) |
| 94 | // - takeDownCount = 0 |
| 95 | // - currentBottles < 0 |
| 96 | // - Special text for 0 bottles ("No more bottles", "no more bottles") |
| 97 | // - Special text for 1 bottle ("1 bottle") |
| 98 | // - Special action text for 1 bottle ("Take it down") |
| 99 | // - Special action text for 0 bottles ("Go to the store") |
| 100 | // - Wrapping around from 0 to 99 bottles |
| 101 | // - Empty line between verses |