| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | | * @param {number} initialBottlesCount |
| 8 | | * @param {number} takeDownCount |
| 9 | | * @return {string[]} |
| 7 | * Generates verses of 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 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: Invalid input types |
| 15 | if (typeof initialBottlesCount !== 'number' || typeof takeDownCount !== 'number') { |
| 16 | throw new Error('Both parameters must be numbers'); |
| 17 | } |
| 18 | |
| 19 | // Edge Case: Negative takeDownCount |
| 20 | if (takeDownCount < 0) { |
| 21 | throw new Error('takeDownCount must be non-negative'); |
| 22 | } |
| 23 | |
| 24 | // Edge Case: initialBottlesCount out of range |
| 25 | if (initialBottlesCount < 0 || initialBottlesCount > 99) { |
| 26 | throw new Error('initialBottlesCount must be between 0 and 99'); |
| 27 | } |
| 28 | |
| 29 | const verses = []; |
| 30 | |
| 31 | for (let i = 0; i < takeDownCount; i++) { |
| 32 | const currentBottles = initialBottlesCount - i; |
| 33 | |
| 34 | // Edge Case: Going below zero bottles |
| 35 | if (currentBottles < 0) { |
| 36 | break; |
| 37 | } |
| 38 | |
| 39 | // Add the verse |
| 40 | const verseLines = createVerse(currentBottles); |
| 41 | verses.push(...verseLines); |
| 42 | |
| 43 | // Add blank line between verses (except after the last verse) |
| 44 | if (i < takeDownCount - 1) { |
| 45 | verses.push(''); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | return verses; |
| 13 | 50 | }; |
| 51 | |
| 52 | /** |
| 53 | * Creates a single verse of the beer song based on the number of bottles. |
| 54 | * |
| 55 | * @param {number} bottles - Number of bottles for this verse |
| 56 | * @return {string[]} - Array containing the two lines of the verse |
| 57 | */ |
| 58 | const createVerse = (bottles) => { |
| 59 | // Edge Case: No bottles left |
| 60 | if (bottles === 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: One bottle left |
| 68 | if (bottles === 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: Two bottles left |
| 76 | if (bottles === 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 | `${bottles} bottles of beer on the wall, ${bottles} bottles of beer.`, |
| 86 | `Take one down and pass it around, ${bottles - 1} bottles of beer on the wall.` |
| 87 | ]; |
| 88 | }; |
| 89 | |
| 90 | // Handled Edge Cases: Invalid input types, negative takeDownCount, initialBottlesCount out of range, going below zero bottles, no bottles left, one bottle left, two bottles left |