| 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 - How many 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: 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 takeDownCount |
| 20 | if (takeDownCount < 0) { |
| 21 | throw new Error('takeDownCount must be non-negative'); |
| 22 | } |
| 23 | |
| 24 | // Edge Case: initialBottlesCount out of range |
| 25 | if (initialBottlesCount < 0 || initialBottlesCount > 99) { |
| 26 | throw new Error('initialBottlesCount must be between 0 and 99'); |
| 27 | } |
| 28 | |
| 29 | // Edge Case: takeDownCount is 0 |
| 30 | if (takeDownCount === 0) { |
| 31 | return []; |
| 32 | } |
| 33 | |
| 34 | const verses = []; |
| 35 | |
| 36 | for (let i = 0; i < takeDownCount; i++) { |
| 37 | const currentBottles = initialBottlesCount - i; |
| 38 | |
| 39 | // Edge Case: currentBottles becomes negative |
| 40 | if (currentBottles < 0) { |
| 41 | break; |
| 42 | } |
| 43 | |
| 44 | let line1, line2; |
| 45 | |
| 46 | if (currentBottles === 0) { |
| 47 | line1 = "No more bottles of beer on the wall, no more bottles of beer."; |
| 48 | line2 = "Go to the store and buy some more, 99 bottles of beer on the wall."; |
| 49 | } else { |
| 50 | // Handle singular vs plural for bottles |
| 51 | const bottleText1 = currentBottles === 1 ? 'bottle' : 'bottles'; |
| 52 | const pronoun = currentBottles === 1 ? 'it' : 'one'; |
| 53 | |
| 54 | line1 = `${currentBottles} ${bottleText1} of beer on the wall, ${currentBottles} ${bottleText1} of beer.`; |
| 55 | |
| 56 | // Handle next number of bottles |
| 57 | const nextBottles = currentBottles - 1; |
| 58 | if (nextBottles === 0) { |
| 59 | line2 = `Take ${pronoun} down and pass it around, no more bottles of beer on the wall.`; |
| 60 | } else { |
| 61 | const bottleText2 = nextBottles === 1 ? 'bottle' : 'bottles'; |
| 62 | line2 = `Take ${pronoun} down and pass it around, ${nextBottles} ${bottleText2} of beer on the wall.`; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | verses.push(line1, line2); |
| 67 | |
| 68 | // Add empty string for spacing between verses, except for the last verse |
| 69 | if (i < takeDownCount - 1) { |
| 70 | verses.push(""); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return verses; |
| 13 | 75 | }; |
| 76 | |
| 77 | // Handled Edge Cases: Invalid input types, Negative takeDownCount, initialBottlesCount out of range, takeDownCount is 0, currentBottles becomes negative |