| 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 | let currentBottles = initialBottlesCount; |
| 30 | |
| 31 | for (let i = 0; i < takeDownCount; i++) { |
| 32 | // Edge Case: Current bottles count goes below 0 |
| 33 | if (currentBottles < 0) { |
| 34 | break; |
| 35 | } |
| 36 | |
| 37 | // Generate the first line of the verse |
| 38 | const bottleWord = currentBottles === 1 ? 'bottle' : 'bottles'; |
| 39 | const firstLine = `${currentBottles === 0 ? 'No more' : currentBottles} ${bottleWord} of beer on the wall, ${currentBottles === 0 ? 'no more' : currentBottles} ${bottleWord} of beer.`; |
| 40 | result.push(firstLine); |
| 41 | |
| 42 | // Generate the second line of the verse |
| 43 | let secondLine; |
| 44 | if (currentBottles === 0) { |
| 45 | // Special case for 0 bottles |
| 46 | secondLine = 'Go to the store and buy some more, 99 bottles of beer on the wall.'; |
| 47 | } else if (currentBottles === 1) { |
| 48 | // Special case for 1 bottle |
| 49 | secondLine = 'Take it down and pass it around, no more bottles of beer on the wall.'; |
| 50 | } else { |
| 51 | // General case |
| 52 | const nextBottles = currentBottles - 1; |
| 53 | const nextBottleWord = nextBottles === 1 ? 'bottle' : 'bottles'; |
| 54 | secondLine = `Take one down and pass it around, ${nextBottles === 0 ? 'no more' : nextBottles} ${nextBottleWord} of beer on the wall.`; |
| 55 | } |
| 56 | result.push(secondLine); |
| 57 | |
| 58 | // Add an empty line between verses except after the last verse |
| 59 | if (i < takeDownCount - 1) { |
| 60 | result.push(''); |
| 61 | } |
| 62 | |
| 63 | currentBottles--; |
| 64 | } |
| 65 | |
| 66 | return result; |
| 13 | 67 | }; |
| 68 | |
| 69 | // Handled Edge Cases: Invalid input types, negative numbers, takeDownCount is 0, current bottles count goes below 0 |