| 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 | const result = []; |
| 13 | |
| 14 | // Edge Case: Handle when takeDownCount is 0 |
| 15 | if (takeDownCount === 0) { |
| 16 | return []; |
| 17 | } |
| 18 | |
| 19 | // Edge Case: Handle when initialBottlesCount is negative |
| 20 | if (initialBottlesCount < 0) { |
| 21 | initialBottlesCount = 0; |
| 22 | } |
| 23 | |
| 24 | for (let i = 0; i < takeDownCount; i++) { |
| 25 | const currentBottles = initialBottlesCount - i; |
| 26 | |
| 27 | // Edge Case: Handle when currentBottles becomes negative during iteration |
| 28 | if (currentBottles < 0) { |
| 29 | break; |
| 30 | } |
| 31 | |
| 32 | let verse; |
| 33 | |
| 34 | if (currentBottles === 0) { |
| 35 | // Special case for 0 bottles |
| 36 | verse = [ |
| 37 | "No more bottles of beer on the wall, no more bottles of beer.", |
| 38 | "Go to the store and buy some more, 99 bottles of beer on the wall." |
| 39 | ]; |
| 40 | } else if (currentBottles === 1) { |
| 41 | // Special case for 1 bottle |
| 42 | verse = [ |
| 43 | "1 bottle of beer on the wall, 1 bottle of beer.", |
| 44 | "Take it down and pass it around, no more bottles of beer on the wall." |
| 45 | ]; |
| 46 | } else if (currentBottles === 2) { |
| 47 | // Special case for 2 bottles (singular in second line) |
| 48 | verse = [ |
| 49 | "2 bottles of beer on the wall, 2 bottles of beer.", |
| 50 | "Take one down and pass it around, 1 bottle of beer on the wall." |
| 51 | ]; |
| 52 | } else { |
| 53 | // General case for 3 or more bottles |
| 54 | verse = [ |
| 55 | `${currentBottles} bottles of beer on the wall, ${currentBottles} bottles of beer.`, |
| 56 | `Take one down and pass it around, ${currentBottles - 1} bottles of beer on the wall.` |
| 57 | ]; |
| 58 | } |
| 59 | |
| 60 | result.push(...verse); |
| 61 | |
| 62 | // Edge Case: Add empty string after each verse except the last one |
| 63 | if (i < takeDownCount - 1) { |
| 64 | result.push(""); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return result; |
| 13 | 69 | }; |
| 70 | |
| 71 | // Handled Edge Cases: takeDownCount is 0, initialBottlesCount is negative, currentBottles becomes negative during iteration, special grammar for 0 bottles, special grammar for 1 bottle, special grammar for 2 bottles, adding empty string between verses |