| 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 bottles. |
| 8 | * |
| 9 | * @param {number} initialBottlesCount - The starting number of bottles (0-99) |
| 10 | * @param {number} takeDownCount - How many verses to recite (default: 1) |
| 11 | * @return {string[]} - Array of lyrics lines |
| 10 | 12 | */ |
| 11 | 13 | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 14 | // Edge Case: Invalid input types |
| 15 | if (typeof initialBottlesCount !== 'number' || typeof takeDownCount !== 'number') { |
| 16 | throw new Error('Both parameters must be numbers'); |
| 17 | } |
| 18 | |
| 19 | // Edge Case: Negative initial bottles count |
| 20 | if (initialBottlesCount < 0) { |
| 21 | throw new Error('Initial bottles count cannot be negative'); |
| 22 | } |
| 23 | |
| 24 | // Edge Case: Negative take down count |
| 25 | if (takeDownCount < 0) { |
| 26 | throw new Error('Take down count cannot be negative'); |
| 27 | } |
| 28 | |
| 29 | // Edge Case: Initial bottles count greater than 99 |
| 30 | if (initialBottlesCount > 99) { |
| 31 | throw new Error('Initial bottles count cannot exceed 99'); |
| 32 | } |
| 33 | |
| 34 | const result = []; |
| 35 | |
| 36 | for (let i = 0; i < takeDownCount; i++) { |
| 37 | const currentBottles = initialBottlesCount - i; |
| 38 | |
| 39 | // Edge Case: Current bottles count becomes negative |
| 40 | if (currentBottles < 0) { |
| 41 | break; |
| 42 | } |
| 43 | |
| 44 | // Handle the "no more bottles" verse |
| 45 | if (currentBottles === 0) { |
| 46 | result.push('No more bottles of beer on the wall, no more bottles of beer.'); |
| 47 | result.push('Go to the store and buy some more, 99 bottles of beer on the wall.'); |
| 48 | } |
| 49 | // Handle the "1 bottle" verse |
| 50 | else if (currentBottles === 1) { |
| 51 | result.push('1 bottle of beer on the wall, 1 bottle of beer.'); |
| 52 | result.push('Take it down and pass it around, no more bottles of beer on the wall.'); |
| 53 | } |
| 54 | // Handle the "2 bottles" verse (special case for pluralization) |
| 55 | else if (currentBottles === 2) { |
| 56 | result.push('2 bottles of beer on the wall, 2 bottles of beer.'); |
| 57 | result.push('Take one down and pass it around, 1 bottle of beer on the wall.'); |
| 58 | } |
| 59 | // Handle all other verses |
| 60 | else { |
| 61 | result.push(`${currentBottles} bottles of beer on the wall, ${currentBottles} bottles of beer.`); |
| 62 | result.push(`Take one down and pass it around, ${currentBottles - 1} bottles of beer on the wall.`); |
| 63 | } |
| 64 | |
| 65 | // Add empty line between verses except after the last verse |
| 66 | if (i < takeDownCount - 1) { |
| 67 | result.push(''); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return result; |
| 72 | |
| 73 | // Handled Edge Cases: |
| 74 | // - Invalid input types |
| 75 | // - Negative initial bottles count |
| 76 | // - Negative take down count |
| 77 | // - Initial bottles count greater than 99 |
| 78 | // - Current bottles count becomes negative |
| 79 | // - Special case for 0 bottles (no more bottles) |
| 80 | // - Special case for 1 bottle |
| 81 | // - Special case for 2 bottles (pluralization) |
| 82 | // - Proper pluralization for all other numbers |
| 83 | // - Correct formatting with empty lines between verses |
| 13 | 84 | }; |