| 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 | |
| 28 | // Edge Case: currentBottles is less than 0 (should not happen with valid inputs but added for robustness) |
| 29 | if (currentBottles < 0) { |
| 30 | break; |
| 31 | } |
| 32 | |
| 33 | // Handle special cases for grammar |
| 34 | const bottleWord = currentBottles === 1 ? 'bottle' : 'bottles'; |
| 35 | const nextBottles = currentBottles - 1; |
| 36 | |
| 37 | // Edge Case: When there are no bottles left (0 bottles) |
| 38 | if (currentBottles === 0) { |
| 39 | result.push('No more bottles of beer on the wall, no more bottles of beer.'); |
| 40 | result.push('Go to the store and buy some more, 99 bottles of beer on the wall.'); |
| 41 | } |
| 42 | // Edge Case: When there is one bottle left |
| 43 | else if (currentBottles === 1) { |
| 44 | result.push('1 bottle of beer on the wall, 1 bottle of beer.'); |
| 45 | result.push('Take it down and pass it around, no more bottles of beer on the wall.'); |
| 46 | } |
| 47 | // Edge Case: When there are two bottles left |
| 48 | else if (currentBottles === 2) { |
| 49 | result.push('2 bottles of beer on the wall, 2 bottles of beer.'); |
| 50 | result.push('Take one down and pass it around, 1 bottle of beer on the wall.'); |
| 51 | } |
| 52 | // General case for more than 2 bottles |
| 53 | else { |
| 54 | result.push(`${currentBottles} bottles of beer on the wall, ${currentBottles} bottles of beer.`); |
| 55 | result.push(`Take one down and pass it around, ${nextBottles} bottles of beer on the wall.`); |
| 56 | } |
| 57 | |
| 58 | // Add empty string for spacing between verses, except after the last verse |
| 59 | // Edge Case: Don't add empty line after the last verse |
| 60 | if (i < takeDownCount - 1) { |
| 61 | result.push(''); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return result; |
| 13 | 66 | }; |
| 67 | |
| 68 | // Handled Edge Cases: Invalid input types or negative values, takeDownCount is 0, currentBottles is less than 0, 0 bottles, 1 bottle, 2 bottles, don't add empty line after last verse |