| 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 cannot be 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 0 bottles |
| 35 | if (currentBottles < 0) { |
| 36 | break; |
| 37 | } |
| 38 | |
| 39 | const verse = createVerse(currentBottles); |
| 40 | const lines = verse.split('\n'); |
| 41 | |
| 42 | // Add the lines of the current verse |
| 43 | verses.push(...lines); |
| 44 | |
| 45 | // Add blank line between verses (but not after the last verse) |
| 46 | if (i < takeDownCount - 1 && currentBottles > 0) { |
| 47 | verses.push(''); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | return verses; |
| 13 | 52 | }; |
| 53 | |
| 54 | /** |
| 55 | * Creates a single verse of the beer song based on the bottle count. |
| 56 | * |
| 57 | * @param {number} bottles - Number of bottles for this verse |
| 58 | * @return {string} - The complete verse |
| 59 | */ |
| 60 | const createVerse = (bottles) => { |
| 61 | // Edge Case: 0 bottles (special verse) |
| 62 | if (bottles === 0) { |
| 63 | return 'No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.'; |
| 64 | } |
| 65 | |
| 66 | // Edge Case: 1 bottle (singular form) |
| 67 | if (bottles === 1) { |
| 68 | return '1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no more bottles of beer on the wall.'; |
| 69 | } |
| 70 | |
| 71 | // Edge Case: 2 bottles (transition from plural to singular) |
| 72 | if (bottles === 2) { |
| 73 | return '2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, 1 bottle of beer on the wall.'; |
| 74 | } |
| 75 | |
| 76 | // General case: more than 2 bottles |
| 77 | return `${bottles} bottles of beer on the wall, ${bottles} bottles of beer.\nTake one down and pass it around, ${bottles - 1} bottles of beer on the wall.`; |
| 78 | }; |
| 79 | |
| 80 | // Handled Edge Cases: Invalid input types, negative takeDownCount, initialBottlesCount out of range, going below 0 bottles, 0 bottles special verse, 1 bottle singular form, 2 bottles transition case |