| 1 | | // |
| 2 | | // This is only a SKELETON file for the 'Beer Song' exercise. It's been provided as a |
| 3 | | // convenience to get you started writing code faster. |
| 4 | | // |
| 5 | | |
| 6 | | /** |
| 7 | | * @param {number} initialBottlesCount |
| 8 | | * @param {number} takeDownCount |
| 9 | | * @return {string[]} |
| 1 | /* |
| 2 | * Generates verses of the '99 Bottles of Beer' song. |
| 3 | * |
| 4 | * @param {number} initialBottlesCount - The starting number of bottles (0-99). |
| 5 | * @param {number} takeDownCount - How many verses to generate (default: 1). |
| 6 | * @return {string[]} - Array of verses for the song. |
| 10 | 7 | */ |
| 11 | 8 | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 9 | // Edge Case: Invalid input types |
| 10 | if (typeof initialBottlesCount !== 'number' || typeof takeDownCount !== 'number') { |
| 11 | throw new Error('Both parameters must be numbers'); |
| 12 | } |
| 13 | |
| 14 | // Edge Case: Negative takeDownCount |
| 15 | if (takeDownCount < 0) { |
| 16 | throw new Error('takeDownCount cannot be negative'); |
| 17 | } |
| 18 | |
| 19 | // Edge Case: initialBottlesCount out of range |
| 20 | if (initialBottlesCount < 0 || initialBottlesCount > 99) { |
| 21 | throw new Error('initialBottlesCount must be between 0 and 99'); |
| 22 | } |
| 23 | |
| 24 | // Edge Case: takeDownCount is 0 |
| 25 | if (takeDownCount === 0) { |
| 26 | return []; |
| 27 | } |
| 28 | |
| 29 | const verses = []; |
| 30 | let currentBottles = initialBottlesCount; |
| 31 | |
| 32 | for (let i = 0; i < takeDownCount; i++) { |
| 33 | // Edge Case: currentBottles becomes negative (should not happen with valid inputs) |
| 34 | if (currentBottles < 0) { |
| 35 | break; |
| 36 | } |
| 37 | |
| 38 | // Handle special cases for different bottle counts |
| 39 | if (currentBottles === 0) { |
| 40 | verses.push('No more bottles of beer on the wall, no more bottles of beer.'); |
| 41 | verses.push('Go to the store and buy some more, 99 bottles of beer on the wall.'); |
| 42 | } else if (currentBottles === 1) { |
| 43 | verses.push('1 bottle of beer on the wall, 1 bottle of beer.'); |
| 44 | verses.push('Take it down and pass it around, no more bottles of beer on the wall.'); |
| 45 | } else if (currentBottles === 2) { |
| 46 | verses.push('2 bottles of beer on the wall, 2 bottles of beer.'); |
| 47 | verses.push('Take one down and pass it around, 1 bottle of beer on the wall.'); |
| 48 | } else { |
| 49 | verses.push(`${currentBottles} bottles of beer on the wall, ${currentBottles} bottles of beer.`); |
| 50 | verses.push(`Take one down and pass it around, ${currentBottles - 1} bottles of beer on the wall.`); |
| 51 | } |
| 52 | |
| 53 | // Add an empty string for spacing between verses, except after the last verse |
| 54 | if (i < takeDownCount - 1) { |
| 55 | verses.push(''); |
| 56 | } |
| 57 | |
| 58 | currentBottles--; |
| 59 | } |
| 60 | |
| 61 | return verses; |
| 13 | 62 | }; |
| 63 | |
| 64 | // Handled Edge Cases: Invalid input types, Negative takeDownCount, initialBottlesCount out of range, takeDownCount is 0, currentBottles becomes negative |