| 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: 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 initial bottle count |
| 20 | if (initialBottlesCount < 0) { |
| 21 | throw new Error('Initial bottle count cannot be negative'); |
| 22 | } |
| 23 | |
| 24 | // Edge Case: Negative take down count |
| 25 | if (takeDownCount < 0) { |
| 26 | throw new Error('Take down count cannot be negative'); |
| 27 | } |
| 28 | |
| 29 | // Edge Case: Initial bottle count greater than 99 |
| 30 | if (initialBottlesCount > 99) { |
| 31 | throw new Error('Initial bottle count cannot be greater than 99'); |
| 32 | } |
| 33 | |
| 34 | const verses = []; |
| 35 | |
| 36 | for (let i = 0; i < takeDownCount; i++) { |
| 37 | const currentBottles = initialBottlesCount - i; |
| 38 | |
| 39 | // Edge Case: Current bottles count becomes less than 0 |
| 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 if (currentBottles === 1) { |
| 50 | line1 = "1 bottle of beer on the wall, 1 bottle of beer."; |
| 51 | line2 = "Take it down and pass it around, no more bottles of beer on the wall."; |
| 52 | } else { |
| 53 | line1 = `${currentBottles} bottles of beer on the wall, ${currentBottles} bottles of beer.`; |
| 54 | const nextBottles = currentBottles - 1; |
| 55 | if (nextBottles === 1) { |
| 56 | line2 = "Take one down and pass it around, 1 bottle of beer on the wall."; |
| 57 | } else { |
| 58 | line2 = `Take one down and pass it around, ${nextBottles} bottles of beer on the wall.`; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | verses.push(line1); |
| 63 | verses.push(line2); |
| 64 | |
| 65 | // Edge Case: Don't add empty line after the last verse |
| 66 | if (i < takeDownCount - 1) { |
| 67 | verses.push(""); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return verses; |
| 72 | |
| 73 | // Handled Edge Cases: |
| 74 | // - Invalid input types |
| 75 | // - Negative initial bottle count |
| 76 | // - Negative take down count |
| 77 | // - Initial bottle count greater than 99 |
| 78 | // - Current bottles count becomes less than 0 |
| 79 | // - Don't add empty line after the last verse |
| 13 | 80 | }; |