| 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 verses = []; |
| 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 | let currentBottleText = ''; |
| 44 | let nextBottleText = ''; |
| 45 | let actionText = ''; |
| 46 | |
| 47 | if (currentBottles === 0) { |
| 48 | // Edge Case: No more bottles |
| 49 | currentBottleText = 'No more bottles'; |
| 50 | actionText = 'Go to the store and buy some more'; |
| 51 | nextBottleText = '99 bottles'; |
| 52 | } else if (currentBottles === 1) { |
| 53 | // Edge Case: One bottle |
| 54 | currentBottleText = '1 bottle'; |
| 55 | actionText = 'Take it down and pass it around'; |
| 56 | nextBottleText = 'no more bottles'; |
| 57 | } else { |
| 58 | // Normal case: Multiple bottles |
| 59 | currentBottleText = `${currentBottles} bottles`; |
| 60 | actionText = 'Take one down and pass it around'; |
| 61 | if (nextBottles === 1) { |
| 62 | nextBottleText = '1 bottle'; |
| 63 | } else if (nextBottles === 0) { |
| 64 | nextBottleText = 'no more bottles'; |
| 65 | } else { |
| 66 | nextBottleText = `${nextBottles} bottles`; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | verses.push(`${currentBottleText} of beer on the wall, ${currentBottleText.toLowerCase()} of beer.`); |
| 71 | |
| 72 | if (currentBottles === 0) { |
| 73 | verses.push(`${actionText}, ${nextBottleText} of beer on the wall.`); |
| 74 | } else { |
| 75 | verses.push(`${actionText}, ${nextBottleText} of beer on the wall.`); |
| 76 | } |
| 77 | |
| 78 | // Add empty line between verses except for the last verse |
| 79 | if (i < takeDownCount - 1) { |
| 80 | verses.push(''); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return verses; |
| 13 | 85 | }; |
| 86 | |
| 87 | // Handled Edge Cases: Invalid input types, Negative initial bottles count, Negative take down count, takeDownCount is 0, Current bottles count is less than 0, No more bottles, One bottle |