| 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' |
| 8 | * @param {number} initialBottlesCount - The starting number of bottles (0-99) |
| 9 | * @param {number} takeDownCount - How many verses to recite (default: 1) |
| 10 | * @return {string[]} - Array of lyrics lines |
| 10 | 11 | */ |
| 11 | 12 | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 13 | // Edge Case: Invalid input types |
| 14 | if (typeof initialBottlesCount !== 'number' || typeof takeDownCount !== 'number') { |
| 15 | throw new Error('Both parameters must be numbers'); |
| 16 | } |
| 17 | |
| 18 | // Edge Case: Negative numbers |
| 19 | if (initialBottlesCount < 0 || takeDownCount < 0) { |
| 20 | throw new Error('Parameters must be non-negative'); |
| 21 | } |
| 22 | |
| 23 | // Edge Case: takeDownCount is 0 |
| 24 | if (takeDownCount === 0) { |
| 25 | return []; |
| 26 | } |
| 27 | |
| 28 | const result = []; |
| 29 | |
| 30 | for (let i = 0; i < takeDownCount; i++) { |
| 31 | const currentBottles = initialBottlesCount - i; |
| 32 | |
| 33 | // Edge Case: Going below 0 bottles |
| 34 | if (currentBottles < 0) { |
| 35 | break; |
| 36 | } |
| 37 | |
| 38 | // Handle the special "no more bottles" case |
| 39 | if (currentBottles === 0) { |
| 40 | result.push('No more bottles of beer on the wall, no more bottles of beer.'); |
| 41 | result.push('Go to the store and buy some more, 99 bottles of beer on the wall.'); |
| 42 | } else { |
| 43 | // Determine bottle text (singular vs plural) |
| 44 | const bottleText = currentBottles === 1 ? 'bottle' : 'bottles'; |
| 45 | const nextBottleCount = currentBottles - 1; |
| 46 | const nextBottleText = nextBottleCount === 1 ? 'bottle' : 'bottles'; |
| 47 | |
| 48 | // Handle the special case for 1 bottle (different action verb) |
| 49 | if (currentBottles === 1) { |
| 50 | result.push('1 bottle of beer on the wall, 1 bottle of beer.'); |
| 51 | result.push('Take it down and pass it around, no more bottles of beer on the wall.'); |
| 52 | } else if (nextBottleCount === 1) { |
| 53 | // When going from 2 to 1 bottle |
| 54 | result.push(`${currentBottles} bottles of beer on the wall, ${currentBottles} bottles of beer.`); |
| 55 | result.push(`Take one down and pass it around, 1 bottle of beer on the wall.`); |
| 56 | } else if (nextBottleCount === 0) { |
| 57 | // When going from 1 to 0 bottles |
| 58 | result.push('1 bottle of beer on the wall, 1 bottle of beer.'); |
| 59 | result.push('Take it down and pass it around, no more bottles of beer on the wall.'); |
| 60 | } else { |
| 61 | // Standard case |
| 62 | result.push(`${currentBottles} bottles of beer on the wall, ${currentBottles} bottles of beer.`); |
| 63 | result.push(`Take one down and pass it around, ${nextBottleCount} bottles of beer on the wall.`); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // Add empty line between verses, except after the last verse |
| 68 | if (i < takeDownCount - 1 && currentBottles > 0) { |
| 69 | result.push(''); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | return result; |
| 13 | 74 | }; |
| 75 | |
| 76 | // Handled Edge Cases: Invalid input types, negative numbers, takeDownCount is 0, going below 0 bottles, singular/plural bottle text, special case for 1 bottle, empty line between verses |