| 1 | | // |
| 2 | | // This is only a SKELETON file for the 'Beer Song' exercise. It's been provided as a |
| 3 | | // convenience to get you started writing code faster. |
| 4 | | // |
| 5 | | |
| 6 | | /** |
| 7 | | * @param {number} initialBottlesCount |
| 8 | | * @param {number} takeDownCount |
| 9 | | * @return {string[]} |
| 10 | | */ |
| 11 | 1 | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 2 | const verses = []; |
| 3 | |
| 4 | // Edge Case: Handle when takeDownCount is 0 |
| 5 | if (takeDownCount === 0) { |
| 6 | return []; |
| 7 | } |
| 8 | |
| 9 | // Edge Case: Handle negative initialBottlesCount |
| 10 | if (initialBottlesCount < 0) { |
| 11 | initialBottlesCount = 0; |
| 12 | } |
| 13 | |
| 14 | // Edge Case: Handle negative takeDownCount |
| 15 | if (takeDownCount < 0) { |
| 16 | takeDownCount = 0; |
| 17 | } |
| 18 | |
| 19 | for (let i = 0; i < takeDownCount; i++) { |
| 20 | const currentBottles = initialBottlesCount - i; |
| 21 | const nextBottles = currentBottles - 1; |
| 22 | |
| 23 | // Edge Case: Handle when currentBottles is 0 (restart verse) |
| 24 | if (currentBottles === 0) { |
| 25 | verses.push("No more bottles of beer on the wall, no more bottles of beer."); |
| 26 | verses.push("Go to the store and buy some more, 99 bottles of beer on the wall."); |
| 27 | // Edge Case: Handle when we've reached the restart verse but still have more iterations |
| 28 | if (i < takeDownCount - 1) { |
| 29 | verses.push(""); |
| 30 | } |
| 31 | continue; |
| 32 | } |
| 33 | |
| 34 | // Edge Case: Handle singular vs plural for current bottles |
| 35 | const currentBottleText = currentBottles === 1 ? "1 bottle" : `${currentBottles} bottles`; |
| 36 | |
| 37 | // Edge Case: Handle when nextBottles is 0 (last verse before restart) |
| 38 | if (nextBottles === 0) { |
| 39 | verses.push(`${currentBottleText} of beer on the wall, ${currentBottleText} of beer.`); |
| 40 | verses.push("Take it down and pass it around, no more bottles of beer on the wall."); |
| 41 | // Edge Case: Handle when we've reached the last verse but still have more iterations |
| 42 | if (i < takeDownCount - 1) { |
| 43 | verses.push(""); |
| 44 | } |
| 45 | continue; |
| 46 | } |
| 47 | |
| 48 | // Edge Case: Handle when nextBottles is 1 (singular form) |
| 49 | const nextBottleText = nextBottles === 1 ? "1 bottle" : `${nextBottles} bottles`; |
| 50 | |
| 51 | verses.push(`${currentBottleText} of beer on the wall, ${currentBottleText} of beer.`); |
| 52 | verses.push(`Take one down and pass it around, ${nextBottleText} of beer on the wall.`); |
| 53 | |
| 54 | // Edge Case: Handle when we're not at the last verse and need a blank line |
| 55 | if (i < takeDownCount - 1) { |
| 56 | verses.push(""); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return verses; |
| 13 | 61 | }; |
| 62 | |
| 63 | // Handled Edge Cases: takeDownCount is 0, negative initialBottlesCount, negative takeDownCount, currentBottles is 0 (restart verse), singular vs plural for current bottles, nextBottles is 0 (last verse before restart), nextBottles is 1 (singular form), when we've reached the restart verse but still have more iterations, when we've reached the last verse but still have more iterations, when we're not at the last verse and need a blank line |