| 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 verses 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: Handle invalid inputs |
| 15 | if (typeof initialBottlesCount !== 'number' || initialBottlesCount < 0 || initialBottlesCount > 99) { |
| 16 | throw new Error('Initial bottles count must be between 0 and 99'); |
| 17 | } |
| 18 | |
| 19 | if (typeof takeDownCount !== 'number' || takeDownCount < 0) { |
| 20 | throw new Error('Take down count must be a non-negative number'); |
| 21 | } |
| 22 | |
| 23 | const verses = []; |
| 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: Handle when we go below 0 bottles |
| 34 | if (currentBottles < 0) { |
| 35 | break; |
| 36 | } |
| 37 | |
| 38 | if (currentBottles === 0) { |
| 39 | verses.push( |
| 40 | 'No more bottles of beer on the wall, no more bottles of beer.', |
| 41 | 'Go to the store and buy some more, 99 bottles of beer on the wall.' |
| 42 | ); |
| 43 | } else { |
| 44 | const currentBottleText = currentBottles === 1 ? '1 bottle' : `${currentBottles} bottles`; |
| 45 | const nextBottles = currentBottles - 1; |
| 46 | const nextBottleText = nextBottles === 1 ? '1 bottle' : (nextBottles === 0 ? 'no more bottles' : `${nextBottles} bottles`); |
| 47 | const action = currentBottles === 1 ? 'Take it down and pass it around' : 'Take one down and pass it around'; |
| 48 | |
| 49 | verses.push( |
| 50 | `${currentBottleText} of beer on the wall, ${currentBottleText} of beer.`, |
| 51 | `${action}, ${nextBottleText} of beer on the wall.` |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | // Edge Case: Add empty string after each verse except the last one |
| 56 | if (i < takeDownCount - 1) { |
| 57 | verses.push(''); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return verses; |
| 13 | 62 | }; |
| 63 | |
| 64 | // Handled Edge Cases: Invalid inputs, negative bottles count, zero take down count, going below zero bottles, special grammar for 1 bottle, special last verse for 0 bottles, empty lines between verses |