| 1 | | // |
| 2 | | // This is only a SKELETON file for the 'Beer Song' exercise. It's been provided as a |
| 3 | | // convenience to get you started writing code faster. |
| 4 | | // |
| 5 | | |
| 6 | | /** |
| 7 | | * @param {number} initialBottlesCount |
| 8 | | * @param {number} takeDownCount |
| 9 | | * @return {string[]} |
| 1 | /* |
| 2 | * Recites the lyrics to 99 Bottles of Beer on the Wall. |
| 3 | * @param {number} initialBottlesCount - The starting number of bottles (0-99) |
| 4 | * @param {number} takeDownCount - How many verses to recite (default: 1) |
| 5 | * @return {string[]} - Array of verse strings |
| 10 | 6 | */ |
| 11 | 7 | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 8 | // Edge Case: Handle negative initialBottlesCount |
| 9 | if (initialBottlesCount < 0) { |
| 10 | throw new Error('Initial bottles count cannot be negative'); |
| 11 | } |
| 12 | |
| 13 | // Edge Case: Handle negative takeDownCount |
| 14 | if (takeDownCount < 0) { |
| 15 | throw new Error('Take down count cannot be negative'); |
| 16 | } |
| 17 | |
| 18 | // Edge Case: Handle when takeDownCount is 0 |
| 19 | if (takeDownCount === 0) { |
| 20 | return []; |
| 21 | } |
| 22 | |
| 23 | const verses = []; |
| 24 | |
| 25 | for (let i = 0; i < takeDownCount; i++) { |
| 26 | const currentBottles = initialBottlesCount - i; |
| 27 | |
| 28 | // Edge Case: Handle when currentBottles goes below 0 |
| 29 | if (currentBottles < 0) { |
| 30 | break; |
| 31 | } |
| 32 | |
| 33 | let line1, line2; |
| 34 | |
| 35 | if (currentBottles === 0) { |
| 36 | line1 = "No more bottles of beer on the wall, no more bottles of beer."; |
| 37 | line2 = "Go to the store and buy some more, 99 bottles of beer on the wall."; |
| 38 | } else if (currentBottles === 1) { |
| 39 | line1 = "1 bottle of beer on the wall, 1 bottle of beer."; |
| 40 | line2 = "Take it down and pass it around, no more bottles of beer on the wall."; |
| 41 | } else { |
| 42 | line1 = `${currentBottles} bottles of beer on the wall, ${currentBottles} bottles of beer.`; |
| 43 | const nextBottles = currentBottles - 1; |
| 44 | if (nextBottles === 1) { |
| 45 | line2 = "Take one down and pass it around, 1 bottle of beer on the wall."; |
| 46 | } else if (nextBottles === 0) { |
| 47 | line2 = "Take one down and pass it around, no more bottles of beer on the wall."; |
| 48 | } else { |
| 49 | line2 = `Take one down and pass it around, ${nextBottles} bottles of beer on the wall.`; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | verses.push(line1); |
| 54 | verses.push(line2); |
| 55 | |
| 56 | // Add empty line between verses except after the last verse |
| 57 | if (i < takeDownCount - 1) { |
| 58 | verses.push(""); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return verses; |
| 13 | 63 | }; |
| 64 | |
| 65 | // Handled Edge Cases: negative initialBottlesCount, negative takeDownCount, takeDownCount is 0, currentBottles goes below 0 |