| 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' for a range of verses. |
| 8 | * |
| 9 | * @param {number} initialBottlesCount - The starting number of bottles (0-99) |
| 10 | * @param {number} takeDownCount - The number of verses to recite (default: 1) |
| 11 | * @return {string[]} - Array of strings representing the lyrics |
| 10 | 12 | */ |
| 11 | 13 | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 14 | // Edge Case: Invalid input types |
| 15 | if (typeof initialBottlesCount !== 'number' || typeof takeDownCount !== 'number') { |
| 16 | throw new Error('Both parameters must be numbers'); |
| 17 | } |
| 18 | |
| 19 | // Edge Case: Negative values |
| 20 | if (initialBottlesCount < 0 || takeDownCount < 0) { |
| 21 | throw new Error('Bottle counts cannot be negative'); |
| 22 | } |
| 23 | |
| 24 | // Edge Case: takeDownCount of 0 |
| 25 | if (takeDownCount === 0) { |
| 26 | return []; |
| 27 | } |
| 28 | |
| 29 | const verses = []; |
| 30 | |
| 31 | for (let i = 0; i < takeDownCount; i++) { |
| 32 | const currentBottles = initialBottlesCount - i; |
| 33 | |
| 34 | // Edge Case: Going below 0 bottles |
| 35 | if (currentBottles < 0) { |
| 36 | break; |
| 37 | } |
| 38 | |
| 39 | const nextBottles = currentBottles - 1; |
| 40 | |
| 41 | // Generate the first line |
| 42 | let firstLine = ''; |
| 43 | if (currentBottles === 0) { |
| 44 | firstLine = 'No more bottles of beer on the wall, no more bottles of beer.'; |
| 45 | } else if (currentBottles === 1) { |
| 46 | firstLine = '1 bottle of beer on the wall, 1 bottle of beer.'; |
| 47 | } else { |
| 48 | firstLine = `${currentBottles} bottles of beer on the wall, ${currentBottles} bottles of beer.`; |
| 49 | } |
| 50 | |
| 51 | // Generate the second line |
| 52 | let secondLine = ''; |
| 53 | if (currentBottles === 0) { |
| 54 | secondLine = 'Go to the store and buy some more, 99 bottles of beer on the wall.'; |
| 55 | } else if (nextBottles === 0) { |
| 56 | secondLine = 'Take it down and pass it around, no more bottles of beer on the wall.'; |
| 57 | } else if (nextBottles === 1) { |
| 58 | secondLine = 'Take one down and pass it around, 1 bottle of beer on the wall.'; |
| 59 | } else { |
| 60 | secondLine = `Take one down and pass it around, ${nextBottles} bottles of beer on the wall.`; |
| 61 | } |
| 62 | |
| 63 | verses.push(firstLine); |
| 64 | verses.push(secondLine); |
| 65 | |
| 66 | // Add empty string for spacing between verses, except for the last verse |
| 67 | if (i < takeDownCount - 1) { |
| 68 | verses.push(''); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return verses; |
| 13 | 73 | }; |
| 74 | |
| 75 | // Handled Edge Cases: Invalid input types, negative values, takeDownCount of 0, going below 0 bottles |