| 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 | * |
| 4 | * @param {number} initialBottlesCount - The starting number of bottles (0-99) |
| 5 | * @param {number} takeDownCount - How many verses to recite (default: 1) |
| 6 | * @return {string[]} - Array of verse strings |
| 10 | 7 | */ |
| 11 | 8 | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 9 | // Edge Case: Handle negative initialBottlesCount |
| 10 | if (initialBottlesCount < 0) { |
| 11 | throw new Error('Initial bottles count cannot be negative'); |
| 12 | } |
| 13 | |
| 14 | // Edge Case: Handle negative takeDownCount |
| 15 | if (takeDownCount < 0) { |
| 16 | throw new Error('Take down count cannot be negative'); |
| 17 | } |
| 18 | |
| 19 | // Edge Case: Handle when takeDownCount is 0 |
| 20 | if (takeDownCount === 0) { |
| 21 | return []; |
| 22 | } |
| 23 | |
| 24 | // Edge Case: Handle when initialBottlesCount exceeds 99 |
| 25 | if (initialBottlesCount > 99) { |
| 26 | // For this implementation, we'll cap it at 99 as per the classic song |
| 27 | initialBottlesCount = 99; |
| 28 | } |
| 29 | |
| 30 | const verses = []; |
| 31 | |
| 32 | for (let i = 0; i < takeDownCount; i++) { |
| 33 | const currentBottles = initialBottlesCount - i; |
| 34 | |
| 35 | // Edge Case: Handle when currentBottles becomes negative |
| 36 | if (currentBottles < 0) { |
| 37 | break; |
| 38 | } |
| 39 | |
| 40 | let line1, line2; |
| 41 | |
| 42 | if (currentBottles === 0) { |
| 43 | line1 = "No more bottles of beer on the wall, no more bottles of beer."; |
| 44 | line2 = "Go to the store and buy some more, 99 bottles of beer on the wall."; |
| 45 | } else if (currentBottles === 1) { |
| 46 | line1 = "1 bottle of beer on the wall, 1 bottle of beer."; |
| 47 | line2 = "Take it down and pass it around, no more bottles of beer on the wall."; |
| 48 | } else { |
| 49 | line1 = `${currentBottles} bottles of beer on the wall, ${currentBottles} bottles of beer.`; |
| 50 | const nextBottles = currentBottles - 1; |
| 51 | if (nextBottles === 1) { |
| 52 | line2 = "Take one down and pass it around, 1 bottle of beer on the wall."; |
| 53 | } else if (nextBottles === 0) { |
| 54 | line2 = "Take one down and pass it around, no more bottles of beer on the wall."; |
| 55 | } else { |
| 56 | line2 = `Take one down and pass it around, ${nextBottles} bottles of beer on the wall.`; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | verses.push(line1); |
| 61 | verses.push(line2); |
| 62 | |
| 63 | // Add empty string for spacing between verses, except after the last verse |
| 64 | if (i < takeDownCount - 1) { |
| 65 | verses.push(""); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return verses; |
| 13 | 70 | }; |
| 71 | |
| 72 | // Handled Edge Cases: negative initialBottlesCount, negative takeDownCount, takeDownCount=0, initialBottlesCount>99, currentBottles becoming negative |