| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | | * @param {number} initialBottlesCount |
| 8 | | * @param {number} takeDownCount |
| 9 | | * @return {string[]} |
| 7 | * Recites verses of the '99 Bottles of Beer' song. |
| 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 verses |
| 10 | 12 | */ |
| 11 | 13 | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 14 | // Edge Case: Handle invalid inputs |
| 15 | if (typeof initialBottlesCount !== 'number' || typeof takeDownCount !== 'number') { |
| 16 | throw new Error('Both parameters must be numbers'); |
| 17 | } |
| 18 | |
| 19 | // Edge Case: Handle negative numbers |
| 20 | if (initialBottlesCount < 0 || takeDownCount < 0) { |
| 21 | throw new Error('Parameters must be non-negative'); |
| 22 | } |
| 23 | |
| 24 | // Edge Case: Handle when takeDownCount is 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: Handle when we go below 0 bottles |
| 35 | if (currentBottles < 0) { |
| 36 | break; |
| 37 | } |
| 38 | |
| 39 | let line1, line2; |
| 40 | |
| 41 | if (currentBottles === 0) { |
| 42 | line1 = 'No more bottles of beer on the wall, no more bottles of beer.'; |
| 43 | line2 = 'Go to the store and buy some more, 99 bottles of beer on the wall.'; |
| 44 | } else if (currentBottles === 1) { |
| 45 | line1 = '1 bottle of beer on the wall, 1 bottle of beer.'; |
| 46 | line2 = 'Take it down and pass it around, no more bottles of beer on the wall.'; |
| 47 | } else { |
| 48 | line1 = `${currentBottles} bottles of beer on the wall, ${currentBottles} bottles of beer.`; |
| 49 | |
| 50 | if (currentBottles - 1 === 1) { |
| 51 | line2 = 'Take one down and pass it around, 1 bottle of beer on the wall.'; |
| 52 | } else { |
| 53 | line2 = `Take one down and pass it around, ${currentBottles - 1} bottles of beer on the wall.`; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | verses.push(line1, line2); |
| 58 | |
| 59 | // Edge Case: Don't add an empty line after the last verse |
| 60 | if (i < takeDownCount - 1) { |
| 61 | verses.push(''); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return verses; |
| 13 | 66 | }; |
| 67 | |
| 68 | // Handled Edge Cases: invalid inputs, negative numbers, takeDownCount is 0, going below 0 bottles |