| 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 verses = []; |
| 13 | |
| 14 | // Edge Case: Handle negative initial bottle count |
| 15 | if (initialBottlesCount < 0) { |
| 16 | throw new Error('Initial bottle count cannot be negative'); |
| 17 | } |
| 18 | |
| 19 | // Edge Case: Handle negative take down count |
| 20 | if (takeDownCount < 0) { |
| 21 | throw new Error('Take down count cannot be negative'); |
| 22 | } |
| 23 | |
| 24 | // Edge Case: Handle when takeDownCount is 0 |
| 25 | if (takeDownCount === 0) { |
| 26 | return []; |
| 27 | } |
| 28 | |
| 29 | for (let i = 0; i < takeDownCount; i++) { |
| 30 | const currentBottles = initialBottlesCount - i; |
| 31 | const nextBottles = currentBottles - 1; |
| 32 | |
| 33 | // Edge Case: Handle when currentBottles is less than 0 |
| 34 | if (currentBottles < 0) { |
| 35 | break; |
| 36 | } |
| 37 | |
| 38 | let currentBottleText = ''; |
| 39 | let nextBottleText = ''; |
| 40 | let actionText = ''; |
| 41 | |
| 42 | if (currentBottles === 0) { |
| 43 | currentBottleText = 'No more bottles'; |
| 44 | actionText = 'Go to the store and buy some more'; |
| 45 | nextBottleText = '99 bottles'; |
| 46 | } else if (currentBottles === 1) { |
| 47 | currentBottleText = '1 bottle'; |
| 48 | actionText = 'Take it down and pass it around'; |
| 49 | nextBottleText = 'no more bottles'; |
| 50 | } else { |
| 51 | currentBottleText = `${currentBottles} bottles`; |
| 52 | actionText = 'Take one down and pass it around'; |
| 53 | if (nextBottles === 1) { |
| 54 | nextBottleText = '1 bottle'; |
| 55 | } else if (nextBottles === 0) { |
| 56 | nextBottleText = 'no more bottles'; |
| 57 | } else { |
| 58 | nextBottleText = `${nextBottles} bottles`; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | verses.push(`${currentBottleText} of beer on the wall, ${currentBottleText.toLowerCase()} of beer.`); |
| 63 | verses.push(`${actionText}, ${nextBottleText} of beer on the wall.`); |
| 64 | |
| 65 | // Edge Case: Add empty string after each verse except the last one |
| 66 | if (i < takeDownCount - 1) { |
| 67 | verses.push(''); |
| 68 | } |
| 69 | |
| 70 | // Edge Case: Break if we've reached the end of the song |
| 71 | if (currentBottles === 0) { |
| 72 | break; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return verses; |
| 13 | 77 | }; |
| 78 | |
| 79 | // Handled Edge Cases: negative initial bottle count, negative take down count, takeDownCount is 0, currentBottles is less than 0, last verse (0 bottles), single bottle cases (1 bottle), empty string after each verse except last, end of song break |