| 9 | 11 | * @return {string[]} |
| 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 or negative values |
| 15 | if (typeof initialBottlesCount !== 'number' || typeof takeDownCount !== 'number' || initialBottlesCount < 0 || takeDownCount < 0) { |
| 16 | throw new Error('Invalid input: Both parameters must be non-negative numbers'); |
| 17 | } |
| 18 | |
| 19 | // Edge Case: takeDownCount is 0, return empty array |
| 20 | if (takeDownCount === 0) { |
| 21 | return []; |
| 22 | } |
| 23 | |
| 24 | const verses = []; |
| 25 | |
| 26 | // Helper function to generate a single verse |
| 27 | const generateVerse = (num) => { |
| 28 | // Edge Case: Special handling for 0 bottles |
| 29 | if (num === 0) { |
| 30 | return [ |
| 31 | "No more bottles of beer on the wall, no more bottles of beer.", |
| 32 | "Go to the store and buy some more, 99 bottles of beer on the wall." |
| 33 | ]; |
| 34 | } |
| 35 | |
| 36 | // Edge Case: Special handling for 1 bottle |
| 37 | if (num === 1) { |
| 38 | return [ |
| 39 | "1 bottle of beer on the wall, 1 bottle of beer.", |
| 40 | "Take it down and pass it around, no more bottles of beer on the wall." |
| 41 | ]; |
| 42 | } |
| 43 | |
| 44 | // Edge Case: Special handling for 2 bottles |
| 45 | if (num === 2) { |
| 46 | return [ |
| 47 | "2 bottles of beer on the wall, 2 bottles of beer.", |
| 48 | "Take one down and pass it around, 1 bottle of beer on the wall." |
| 49 | ]; |
| 50 | } |
| 51 | |
| 52 | // General case for 3 or more bottles |
| 53 | return [ |
| 54 | `${num} bottles of beer on the wall, ${num} bottles of beer.`, |
| 55 | `Take one down and pass it around, ${num - 1} bottles of beer on the wall.` |
| 56 | ]; |
| 57 | }; |
| 58 | |
| 59 | // Generate the requested verses |
| 60 | for (let i = 0; i < takeDownCount; i++) { |
| 61 | const currentBottles = initialBottlesCount - i; |
| 62 | |
| 63 | // Edge Case: If we go below 0, stop generating verses |
| 64 | if (currentBottles < 0) { |
| 65 | break; |
| 66 | } |
| 67 | |
| 68 | const verse = generateVerse(currentBottles); |
| 69 | verses.push(...verse); |
| 70 | |
| 71 | // Add empty string for spacing between verses, except after the last verse |
| 72 | if (i < takeDownCount - 1) { |
| 73 | verses.push(""); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return verses; |
| 13 | 78 | }; |
| 79 | |
| 80 | // Handled Edge Cases: Invalid input types or negative values, takeDownCount is 0, Special handling for 0 bottles, Special handling for 1 bottle, Special handling for 2 bottles, If we go below 0, stop generating verses |