| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | | * @param {number} initialBottlesCount |
| 8 | | * @param {number} takeDownCount |
| 9 | | * @return {string[]} |
| 7 | * Generates 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 generate (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 cannot be 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 | const verses = []; |
| 30 | |
| 31 | for (let i = 0; i < takeDownCount; i++) { |
| 32 | const currentBottles = initialBottlesCount - i; |
| 33 | |
| 34 | // Edge Case: Going below zero 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 | const nextBottles = currentBottles - 1; |
| 50 | if (nextBottles === 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, ${nextBottles} bottles of beer on the wall.`; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | verses.push(line1, line2); |
| 58 | |
| 59 | // Add empty string for spacing between verses, except for 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 input types, Negative takeDownCount, initialBottlesCount out of range, Going below zero bottles |