| 9 | 9 | * @return {string[]} |
| 10 | 10 | */ |
| 11 | 11 | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 12 | // Edge Case: Invalid input types or negative values |
| 13 | if (typeof initialBottlesCount !== 'number' || typeof takeDownCount !== 'number' || |
| 14 | initialBottlesCount < 0 || takeDownCount < 0) { |
| 15 | throw new Error('Invalid input: counts must be non-negative numbers'); |
| 16 | } |
| 17 | |
| 18 | // Edge Case: takeDownCount is 0, return empty array |
| 19 | if (takeDownCount === 0) { |
| 20 | return []; |
| 21 | } |
| 22 | |
| 23 | const result = []; |
| 24 | |
| 25 | for (let i = 0; i < takeDownCount; i++) { |
| 26 | const currentBottles = initialBottlesCount - i; |
| 27 | const nextBottles = currentBottles - 1; |
| 28 | |
| 29 | // Edge Case: currentBottles is 0 (no more bottles) |
| 30 | if (currentBottles === 0) { |
| 31 | result.push('No more bottles of beer on the wall, no more bottles of beer.'); |
| 32 | result.push('Go to the store and buy some more, 99 bottles of beer on the wall.'); |
| 33 | // Edge Case: If we continue after 0, we would need to restart from 99, but the loop handles only takeDownCount verses |
| 34 | // So we break here to match expected behavior |
| 35 | break; |
| 36 | } |
| 37 | |
| 38 | // Edge Case: currentBottles is 1 (singular form) |
| 39 | else if (currentBottles === 1) { |
| 40 | result.push('1 bottle of beer on the wall, 1 bottle of beer.'); |
| 41 | result.push('Take it down and pass it around, no more bottles of beer on the wall.'); |
| 42 | } |
| 43 | |
| 44 | // Edge Case: currentBottles is 2 (special case for next bottle being singular) |
| 45 | else if (currentBottles === 2) { |
| 46 | result.push('2 bottles of beer on the wall, 2 bottles of beer.'); |
| 47 | result.push('Take one down and pass it around, 1 bottle of beer on the wall.'); |
| 48 | } |
| 49 | |
| 50 | // General case: currentBottles > 2 |
| 51 | else { |
| 52 | result.push(`${currentBottles} bottles of beer on the wall, ${currentBottles} bottles of beer.`); |
| 53 | result.push(`Take one down and pass it around, ${nextBottles} bottles of beer on the wall.`); |
| 54 | } |
| 55 | |
| 56 | // Add empty string for spacing between verses, except after the last verse |
| 57 | // Edge Case: Last verse doesn't need a trailing empty string |
| 58 | if (i < takeDownCount - 1) { |
| 59 | result.push(''); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return result; |
| 13 | 64 | }; |
| 65 | |
| 66 | // Handled Edge Cases: Invalid input types or negative values, takeDownCount is 0, currentBottles is 0 (no more bottles), currentBottles is 1 (singular form), currentBottles is 2 (special case for next bottle being singular), Last verse doesn't need a trailing empty string |