| 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 | | // |
| 1 | /* |
| 2 | * Helper function to generate a single verse of the beer song |
| 3 | * @param {number} bottles - Number of bottles |
| 4 | * @return {string} - The verse lyrics |
| 5 | */ |
| 6 | const generateVerse = (bottles) => { |
| 7 | // Edge Case: When there are no bottles |
| 8 | if (bottles === 0) { |
| 9 | return [ |
| 10 | 'No more bottles of beer on the wall, no more bottles of beer.', |
| 11 | 'Go to the store and buy some more, 99 bottles of beer on the wall.' |
| 12 | ]; |
| 13 | } |
| 14 | |
| 15 | // Edge Case: When there is only one bottle |
| 16 | if (bottles === 1) { |
| 17 | return [ |
| 18 | '1 bottle of beer on the wall, 1 bottle of beer.', |
| 19 | 'Take it down and pass it around, no more bottles of beer on the wall.' |
| 20 | ]; |
| 21 | } |
| 22 | |
| 23 | // Edge Case: When there are two bottles |
| 24 | if (bottles === 2) { |
| 25 | return [ |
| 26 | '2 bottles of beer on the wall, 2 bottles of beer.', |
| 27 | 'Take one down and pass it around, 1 bottle of beer on the wall.' |
| 28 | ]; |
| 29 | } |
| 30 | |
| 31 | // General case for more than two bottles |
| 32 | return [ |
| 33 | `${bottles} bottles of beer on the wall, ${bottles} bottles of beer.`, |
| 34 | `Take one down and pass it around, ${bottles - 1} bottles of beer on the wall.` |
| 35 | ]; |
| 36 | }; |
| 5 | 37 | |
| 6 | 38 | /** |
| 7 | 39 | * @param {number} initialBottlesCount |
| 9 | 41 | * @return {string[]} |
| 10 | 42 | */ |
| 11 | 43 | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 44 | // Edge Case: Invalid input for initialBottlesCount (negative) |
| 45 | if (initialBottlesCount < 0) { |
| 46 | throw new Error('Initial bottles count cannot be negative'); |
| 47 | } |
| 48 | |
| 49 | // Edge Case: Invalid input for takeDownCount (negative or zero) |
| 50 | if (takeDownCount <= 0) { |
| 51 | throw new Error('Take down count must be positive'); |
| 52 | } |
| 53 | |
| 54 | const result = []; |
| 55 | |
| 56 | // Generate the requested verses |
| 57 | for (let i = 0; i < takeDownCount; i++) { |
| 58 | const currentBottles = initialBottlesCount - i; |
| 59 | |
| 60 | // Edge Case: If we go below 0 bottles, we stop at 0 |
| 61 | if (currentBottles < 0) { |
| 62 | break; |
| 63 | } |
| 64 | |
| 65 | const verse = generateVerse(currentBottles); |
| 66 | result.push(...verse); |
| 67 | |
| 68 | // Add an empty string for spacing between verses, except after the last verse |
| 69 | // Edge Case: Don't add empty line after the last verse in the requested range |
| 70 | if (i < takeDownCount - 1) { |
| 71 | result.push(''); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return result; |
| 13 | 76 | }; |
| 77 | |
| 78 | // Handled Edge Cases: negative initialBottlesCount, non-positive takeDownCount, going below 0 bottles, single bottle case, two bottles case, no bottles case |