| 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: Invalid input types |
| 13 | if (typeof initialBottlesCount !== 'number' || typeof takeDownCount !== 'number') { |
| 14 | throw new Error('Invalid input: both parameters must be numbers'); |
| 15 | } |
| 16 | |
| 17 | // Edge Case: Negative initial bottles count |
| 18 | if (initialBottlesCount < 0) { |
| 19 | throw new Error('Invalid input: initialBottlesCount cannot be negative'); |
| 20 | } |
| 21 | |
| 22 | // Edge Case: Negative take down count |
| 23 | if (takeDownCount < 0) { |
| 24 | throw new Error('Invalid input: takeDownCount cannot be negative'); |
| 25 | } |
| 26 | |
| 27 | // Edge Case: takeDownCount is 0 |
| 28 | if (takeDownCount === 0) { |
| 29 | return []; |
| 30 | } |
| 31 | |
| 32 | const result = []; |
| 33 | |
| 34 | for (let i = 0; i < takeDownCount; i++) { |
| 35 | const currentBottles = initialBottlesCount - i; |
| 36 | const nextBottles = currentBottles - 1; |
| 37 | |
| 38 | // Edge Case: Current bottles count is less than 0 |
| 39 | if (currentBottles < 0) { |
| 40 | break; |
| 41 | } |
| 42 | |
| 43 | // Handle special cases for different bottle counts |
| 44 | let currentBottleText = ''; |
| 45 | let nextBottleText = ''; |
| 46 | let actionText = ''; |
| 47 | |
| 48 | if (currentBottles === 0) { |
| 49 | // Edge Case: No more bottles |
| 50 | currentBottleText = 'No more bottles of beer'; |
| 51 | actionText = 'Go to the store and buy some more'; |
| 52 | nextBottleText = '99 bottles of beer'; |
| 53 | } else if (currentBottles === 1) { |
| 54 | // Edge Case: One bottle |
| 55 | currentBottleText = '1 bottle of beer'; |
| 56 | actionText = 'Take it down and pass it around'; |
| 57 | nextBottleText = 'no more bottles of beer'; |
| 58 | } else { |
| 59 | // Edge Case: Multiple bottles |
| 60 | currentBottleText = `${currentBottles} bottles of beer`; |
| 61 | actionText = 'Take one down and pass it around'; |
| 62 | if (nextBottles === 1) { |
| 63 | nextBottleText = '1 bottle of beer'; |
| 64 | } else if (nextBottles === 0) { |
| 65 | nextBottleText = 'no more bottles of beer'; |
| 66 | } else { |
| 67 | nextBottleText = `${nextBottles} bottles of beer`; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | result.push(`${currentBottleText} on the wall, ${currentBottles === 0 ? 'no more bottles of beer' : currentBottleText}.`); |
| 72 | result.push(`${actionText}, ${nextBottleText} on the wall.`); |
| 73 | |
| 74 | // Add empty line between verses except for the last one |
| 75 | if (i < takeDownCount - 1) { |
| 76 | result.push(''); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return result; |
| 81 | |
| 82 | // Handled Edge Cases: Invalid input types, negative initial bottles count, negative take down count, takeDownCount is 0, current bottles count less than 0, no more bottles, one bottle, multiple bottles |
| 13 | 83 | }; |