| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | | * @param {number} initialBottlesCount |
| 8 | | * @param {number} takeDownCount |
| 9 | | * @return {string[]} |
| 7 | * Generates the lyrics for the beer song starting from a given number of bottles. |
| 8 | * |
| 9 | * @param {number} initialBottlesCount - The initial number of bottles (0-99) |
| 10 | * @param {number} takeDownCount - How many verses to generate (default: 1) |
| 11 | * @return {string[]} - Array of lines for the beer song |
| 10 | 12 | */ |
| 11 | 13 | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 14 | // Edge Case: Validate input parameters |
| 15 | if (initialBottlesCount < 0 || initialBottlesCount > 99) { |
| 16 | throw new Error('Initial bottles count must be between 0 and 99'); |
| 17 | } |
| 18 | |
| 19 | if (takeDownCount < 0) { |
| 20 | throw new Error('Take down count must be non-negative'); |
| 21 | } |
| 22 | |
| 23 | const result = []; |
| 24 | |
| 25 | // Edge Case: Handle when no verses are requested |
| 26 | if (takeDownCount === 0) { |
| 27 | return []; |
| 28 | } |
| 29 | |
| 30 | for (let i = 0; i < takeDownCount; i++) { |
| 31 | const currentBottles = initialBottlesCount - i; |
| 32 | |
| 33 | // Edge Case: Prevent negative bottle counts |
| 34 | if (currentBottles < 0) { |
| 35 | break; |
| 36 | } |
| 37 | |
| 38 | // Add empty line between verses (except for the first verse) |
| 39 | if (i > 0) { |
| 40 | result.push(''); |
| 41 | } |
| 42 | |
| 43 | const verseLines = createVerse(currentBottles); |
| 44 | result.push(...verseLines); |
| 45 | } |
| 46 | |
| 47 | return result; |
| 13 | 48 | }; |
| 49 | |
| 50 | /** |
| 51 | * Creates a single verse of the beer song based on the number of bottles. |
| 52 | * |
| 53 | * @param {number} bottles - Number of bottles for this verse |
| 54 | * @return {string[]} - The complete verse as an array of lines |
| 55 | */ |
| 56 | const createVerse = (bottles) => { |
| 57 | // Edge Case: Handle the special "no more bottles" verse |
| 58 | if (bottles === 0) { |
| 59 | return [ |
| 60 | 'No more bottles of beer on the wall, no more bottles of beer.', |
| 61 | 'Go to the store and buy some more, 99 bottles of beer on the wall.' |
| 62 | ]; |
| 63 | } |
| 64 | |
| 65 | // Edge Case: Handle the special "1 bottle" verse |
| 66 | if (bottles === 1) { |
| 67 | return [ |
| 68 | '1 bottle of beer on the wall, 1 bottle of beer.', |
| 69 | 'Take it down and pass it around, no more bottles of beer on the wall.' |
| 70 | ]; |
| 71 | } |
| 72 | |
| 73 | // Edge Case: Handle the special "2 bottles" verse (singular in second line) |
| 74 | if (bottles === 2) { |
| 75 | return [ |
| 76 | '2 bottles of beer on the wall, 2 bottles of beer.', |
| 77 | 'Take one down and pass it around, 1 bottle of beer on the wall.' |
| 78 | ]; |
| 79 | } |
| 80 | |
| 81 | // General case for 3 or more bottles |
| 82 | return [ |
| 83 | `${bottles} bottles of beer on the wall, ${bottles} bottles of beer.`, |
| 84 | `Take one down and pass it around, ${bottles - 1} bottles of beer on the wall.` |
| 85 | ]; |
| 86 | }; |
| 87 | |
| 88 | // Handled Edge Cases: |
| 89 | // - Invalid initialBottlesCount (< 0 or > 99) |
| 90 | // - Invalid takeDownCount (< 0) |
| 91 | // - takeDownCount = 0 (no verses requested) |
| 92 | // - Negative bottle counts during iteration |
| 93 | // - Special case for 0 bottles (final verse) |
| 94 | // - Special case for 1 bottle (singular form and "it" instead of "one") |
| 95 | // - Special case for 2 bottles (singular form in second line) |
| 96 | // - General case for 3+ bottles |