| 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 | const nextBottles = currentBottles - 1; |
| 39 | |
| 40 | // Generate the first line |
| 41 | const firstLine = `${bottleText(currentBottles)} on the wall, ${bottleText(currentBottles).toLowerCase()}.`; |
| 42 | result.push(firstLine); |
| 43 | |
| 44 | // Generate the second line |
| 45 | let secondLine; |
| 46 | if (currentBottles > 1) { |
| 47 | secondLine = `Take one down and pass it around, ${bottleText(nextBottles).toLowerCase()} on the wall.`; |
| 48 | } else if (currentBottles === 1) { |
| 49 | secondLine = `Take it down and pass it around, ${bottleText(nextBottles).toLowerCase()} on the wall.`; |
| 50 | } else { // currentBottles === 0 |
| 51 | secondLine = 'Go to the store and buy some more, 99 bottles of beer on the wall.'; |
| 52 | } |
| 53 | result.push(secondLine); |
| 54 | |
| 55 | // Add empty line except after the last verse |
| 56 | if (i < takeDownCount - 1) { |
| 57 | result.push(''); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return result; |
| 62 | |
| 63 | // Helper function to generate bottle text |
| 64 | function bottleText(num) { |
| 65 | if (num > 1) { |
| 66 | return `${num} bottles of beer`; |
| 67 | } else if (num === 1) { |
| 68 | return '1 bottle of beer'; |
| 69 | } else { |
| 70 | return 'No more bottles of beer'; |
| 71 | } |
| 72 | } |
| 13 | 73 | }; |
| 74 | |
| 75 | // Handled Edge Cases: Invalid input types, negative numbers, takeDownCount is 0, going below 0 bottles |