| 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 negative initial bottle counts |
| 15 | if (initialBottlesCount < 0) { |
| 16 | initialBottlesCount = 0; |
| 17 | } |
| 18 | |
| 19 | // Edge Case: Handle initial bottle counts greater than 99 |
| 20 | if (initialBottlesCount > 99) { |
| 21 | initialBottlesCount = 99; |
| 22 | } |
| 23 | |
| 24 | // Edge Case: Handle negative take down counts |
| 25 | if (takeDownCount < 0) { |
| 26 | takeDownCount = 1; |
| 27 | } |
| 28 | |
| 29 | const verses = []; |
| 30 | |
| 31 | for (let i = 0; i < takeDownCount; i++) { |
| 32 | const currentBottles = initialBottlesCount - i; |
| 33 | |
| 34 | // Edge Case: Prevent going below 0 bottles |
| 35 | if (currentBottles < 0) { |
| 36 | break; |
| 37 | } |
| 38 | |
| 39 | verses.push(...generateVerse(currentBottles)); |
| 40 | |
| 41 | // Edge Case: Don't add empty line after the last verse |
| 42 | if (i < takeDownCount - 1) { |
| 43 | verses.push(''); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | return verses; |
| 48 | |
| 49 | // Handled Edge Cases: negative initial bottle counts, initial bottle counts > 99, negative take down counts, going below 0 bottles, no trailing empty line |
| 13 | 50 | }; |
| 51 | |
| 52 | /** |
| 53 | * Generates a single verse of the 99 Bottles of Beer song. |
| 54 | * |
| 55 | * @param {number} numBottles - The number of bottles for this verse |
| 56 | * @return {string[]} - Array containing the lines of the verse |
| 57 | */ |
| 58 | function generateVerse(numBottles) { |
| 59 | // Edge Case: Handle 0 bottles (special case) |
| 60 | if (numBottles === 0) { |
| 61 | return [ |
| 62 | 'No more bottles of beer on the wall, no more bottles of beer.', |
| 63 | 'Go to the store and buy some more, 99 bottles of beer on the wall.' |
| 64 | ]; |
| 65 | } |
| 66 | |
| 67 | // Edge Case: Handle 1 bottle (singular form) |
| 68 | if (numBottles === 1) { |
| 69 | return [ |
| 70 | '1 bottle of beer on the wall, 1 bottle of beer.', |
| 71 | 'Take it down and pass it around, no more bottles of beer on the wall.' |
| 72 | ]; |
| 73 | } |
| 74 | |
| 75 | // Edge Case: Handle 2 bottles (transition from plural to singular) |
| 76 | if (numBottles === 2) { |
| 77 | return [ |
| 78 | '2 bottles of beer on the wall, 2 bottles of beer.', |
| 79 | 'Take one down and pass it around, 1 bottle of beer on the wall.' |
| 80 | ]; |
| 81 | } |
| 82 | |
| 83 | // General case for 3 or more bottles |
| 84 | return [ |
| 85 | `${numBottles} bottles of beer on the wall, ${numBottles} bottles of beer.`, |
| 86 | `Take one down and pass it around, ${numBottles - 1} bottles of beer on the wall.` |
| 87 | ]; |
| 88 | |
| 89 | // Handled Edge Cases: 0 bottles, 1 bottle, 2 bottles, 3+ bottles |
| 90 | } |