| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | | * @param {number} initialBottlesCount |
| 8 | | * @param {number} takeDownCount |
| 9 | | * @return {string[]} |
| 7 | * Generates the lyrics for a range of verses in the 99 Bottles of Beer song. |
| 8 | * |
| 9 | * @param {number} initialBottlesCount - The starting number of bottles (0-99) |
| 10 | * @param {number} takeDownCount - How many verses to generate (default: 1) |
| 11 | * @return {string[]} - Array of strings representing the song verses |
| 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 | // Edge Case: Handle when takeDownCount is 0 |
| 24 | if (takeDownCount === 0) { |
| 25 | return []; |
| 26 | } |
| 27 | |
| 28 | const verses = []; |
| 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 | verses.push(...generateVerse(currentBottles)); |
| 39 | |
| 40 | // Edge Case: Don't add empty line after the last verse |
| 41 | if (i < takeDownCount - 1 && currentBottles > 0) { |
| 42 | verses.push(''); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | return verses; |
| 13 | 47 | }; |
| 48 | |
| 49 | /** |
| 50 | * Generates a single verse of the 99 Bottles of Beer song. |
| 51 | * |
| 52 | * @param {number} numBottles - The number of bottles for this verse |
| 53 | * @return {string[]} - Array of strings representing the two lines of the verse |
| 54 | */ |
| 55 | const generateVerse = (numBottles) => { |
| 56 | // Edge Case: Handle 0 bottles (special verse) |
| 57 | if (numBottles === 0) { |
| 58 | return [ |
| 59 | 'No more bottles of beer on the wall, no more bottles of beer.', |
| 60 | 'Go to the store and buy some more, 99 bottles of beer on the wall.' |
| 61 | ]; |
| 62 | } |
| 63 | |
| 64 | // Edge Case: Handle 1 bottle (singular form) |
| 65 | if (numBottles === 1) { |
| 66 | return [ |
| 67 | '1 bottle of beer on the wall, 1 bottle of beer.', |
| 68 | 'Take it down and pass it around, no more bottles of beer on the wall.' |
| 69 | ]; |
| 70 | } |
| 71 | |
| 72 | // Edge Case: Handle 2 bottles (transition from plural to singular) |
| 73 | if (numBottles === 2) { |
| 74 | return [ |
| 75 | '2 bottles of beer on the wall, 2 bottles of beer.', |
| 76 | 'Take one down and pass it around, 1 bottle of beer on the wall.' |
| 77 | ]; |
| 78 | } |
| 79 | |
| 80 | // General case for 3 or more bottles |
| 81 | return [ |
| 82 | `${numBottles} bottles of beer on the wall, ${numBottles} bottles of beer.`, |
| 83 | `Take one down and pass it around, ${numBottles - 1} bottles of beer on the wall.` |
| 84 | ]; |
| 85 | }; |
| 86 | |
| 87 | // Handled Edge Cases: |
| 88 | // - Invalid initialBottlesCount values (< 0 or > 99) |
| 89 | // - Invalid takeDownCount values (< 0) |
| 90 | // - takeDownCount = 0 (returns empty array) |
| 91 | // - Going below 0 bottles during iteration |
| 92 | // - Not adding empty line after last verse |
| 93 | // - Special verse for 0 bottles |
| 94 | // - Singular form for 1 bottle |
| 95 | // - Transition from plural to singular for 2 bottles |
| 96 | // - General case for 3+ bottles |