| 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: Handle negative initial bottle counts |
| 13 | if (initialBottlesCount < 0) { |
| 14 | throw new Error('Initial bottle count cannot be negative'); |
| 15 | } |
| 16 | |
| 17 | // Edge Case: Handle negative take down counts |
| 18 | if (takeDownCount < 0) { |
| 19 | throw new Error('Take down count cannot be negative'); |
| 20 | } |
| 21 | |
| 22 | // Edge Case: Handle when takeDownCount is 0 |
| 23 | if (takeDownCount === 0) { |
| 24 | return []; |
| 25 | } |
| 26 | |
| 27 | const result = []; |
| 28 | |
| 29 | for (let i = 0; i < takeDownCount; i++) { |
| 30 | const currentBottles = initialBottlesCount - i; |
| 31 | const nextBottles = currentBottles - 1; |
| 32 | |
| 33 | // Edge Case: Handle when currentBottles is 0 (no more bottles) |
| 34 | if (currentBottles === 0) { |
| 35 | result.push('No more bottles of beer on the wall, no more bottles of beer.'); |
| 36 | result.push('Go to the store and buy some more, 99 bottles of beer on the wall.'); |
| 37 | |
| 38 | // Edge Case: If we're at the last verse, don't add an extra newline |
| 39 | if (i < takeDownCount - 1) { |
| 40 | result.push(''); |
| 41 | } |
| 42 | continue; |
| 43 | } |
| 44 | |
| 45 | // Determine bottle text for current verse |
| 46 | const currentBottleText = currentBottles === 1 ? '1 bottle' : `${currentBottles} bottles`; |
| 47 | |
| 48 | // Edge Case: Handle when nextBottles is 0 (last bottle) |
| 49 | if (nextBottles === 0) { |
| 50 | result.push(`${currentBottleText} of beer on the wall, ${currentBottleText} of beer.`); |
| 51 | result.push('Take it down and pass it around, no more bottles of beer on the wall.'); |
| 52 | |
| 53 | // Edge Case: If we're at the last verse, don't add an extra newline |
| 54 | if (i < takeDownCount - 1) { |
| 55 | result.push(''); |
| 56 | } |
| 57 | continue; |
| 58 | } |
| 59 | |
| 60 | // Edge Case: Handle when nextBottles is 1 (second to last bottle) |
| 61 | const nextBottleText = nextBottles === 1 ? '1 bottle' : `${nextBottles} bottles`; |
| 62 | |
| 63 | result.push(`${currentBottleText} of beer on the wall, ${currentBottleText} of beer.`); |
| 64 | result.push(`Take one down and pass it around, ${nextBottleText} of beer on the wall.`); |
| 65 | |
| 66 | // Edge Case: If we're at the last verse, don't add an extra newline |
| 67 | if (i < takeDownCount - 1) { |
| 68 | result.push(''); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return result; |
| 13 | 73 | }; |
| 74 | |
| 75 | // Handled Edge Cases: negative initial bottle counts, negative take down counts, takeDownCount is 0, currentBottles is 0 (no more bottles), nextBottles is 0 (last bottle), nextBottles is 1 (second to last bottle), last verse handling |