| 9 | 9 | * @return {string[]} |
| 10 | 10 | */ |
| 11 | 11 | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 12 | // Edge Case: Handle negative initial bottles count |
| 13 | if (initialBottlesCount < 0) { |
| 14 | throw new Error('Initial bottles count cannot be negative'); |
| 15 | } |
| 16 | |
| 17 | // Edge Case: Handle negative take down count |
| 18 | if (takeDownCount < 0) { |
| 19 | throw new Error('Take down count cannot be negative'); |
| 20 | } |
| 21 | |
| 22 | // Edge Case: Handle when takeDownCount is 0 |
| 23 | if (takeDownCount === 0) { |
| 24 | return []; |
| 25 | } |
| 26 | |
| 27 | const verses = []; |
| 28 | |
| 29 | for (let i = 0; i < takeDownCount; i++) { |
| 30 | const currentBottles = initialBottlesCount - i; |
| 31 | |
| 32 | // Edge Case: Handle when currentBottles is less than 0 |
| 33 | if (currentBottles < 0) { |
| 34 | break; |
| 35 | } |
| 36 | |
| 37 | let line1, line2; |
| 38 | |
| 39 | if (currentBottles === 0) { |
| 40 | line1 = "No more bottles of beer on the wall, no more bottles of beer."; |
| 41 | line2 = "Go to the store and buy some more, 99 bottles of beer on the wall."; |
| 42 | } else if (currentBottles === 1) { |
| 43 | line1 = "1 bottle of beer on the wall, 1 bottle of beer."; |
| 44 | line2 = "Take it down and pass it around, no more bottles of beer on the wall."; |
| 45 | } else { |
| 46 | line1 = `${currentBottles} bottles of beer on the wall, ${currentBottles} bottles of beer.`; |
| 47 | |
| 48 | if (currentBottles - 1 === 1) { |
| 49 | line2 = "Take one down and pass it around, 1 bottle of beer on the wall."; |
| 50 | } else { |
| 51 | line2 = `Take one down and pass it around, ${currentBottles - 1} bottles of beer on the wall.`; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | verses.push(line1, line2); |
| 56 | |
| 57 | // Edge Case: Don't add an empty line after the last verse |
| 58 | if (i < takeDownCount - 1) { |
| 59 | verses.push(""); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return verses; |
| 13 | 64 | }; |
| 65 | |
| 66 | // Handled Edge Cases: negative initial bottles count, negative take down count, takeDownCount is 0, currentBottles is less than 0, 0 bottles, 1 bottle, 2 bottles |