| 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 | let currentBottles = initialBottlesCount; |
| 34 | |
| 35 | for (let i = 0; i < takeDownCount; i++) { |
| 36 | // Edge Case: When we reach 0 bottles |
| 37 | if (currentBottles === 0) { |
| 38 | result.push('No more bottles of beer on the wall, no more bottles of beer.'); |
| 39 | result.push('Go to the store and buy some more, 99 bottles of beer on the wall.'); |
| 40 | break; |
| 41 | } |
| 42 | |
| 43 | // Handle singular vs plural for current bottles |
| 44 | const currentBottleText = currentBottles === 1 ? '1 bottle' : `${currentBottles} bottles`; |
| 45 | |
| 46 | // Handle singular vs plural for next bottles |
| 47 | const nextBottles = currentBottles - 1; |
| 48 | let nextBottleText; |
| 49 | if (nextBottles === 0) { |
| 50 | nextBottleText = 'no more bottles'; |
| 51 | } else if (nextBottles === 1) { |
| 52 | nextBottleText = '1 bottle'; |
| 53 | } else { |
| 54 | nextBottleText = `${nextBottles} bottles`; |
| 55 | } |
| 56 | |
| 57 | // Handle "take it down" vs "take one down" |
| 58 | const actionText = currentBottles === 1 ? 'Take it down and pass it around' : 'Take one down and pass it around'; |
| 59 | |
| 60 | result.push(`${currentBottleText} of beer on the wall, ${currentBottleText} of beer.`); |
| 61 | result.push(`${actionText}, ${nextBottleText} of beer on the wall.`); |
| 62 | |
| 63 | // Add empty string for spacing between verses, except for the last verse |
| 64 | if (i < takeDownCount - 1) { |
| 65 | result.push(''); |
| 66 | } |
| 67 | |
| 68 | currentBottles--; |
| 69 | } |
| 70 | |
| 71 | return result; |
| 13 | 72 | }; |
| 73 | |
| 74 | // Handled Edge Cases: Invalid input types, negative initial bottles count, negative take down count, takeDownCount is 0, reaching 0 bottles |