| 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 the lyrics for the beer song starting from a given number of bottles. |
| 3 | * @param {number} initialBottlesCount - The starting number of bottles (0-99). |
| 4 | * @param {number} takeDownCount - The number of verses to generate (default is 1). |
| 5 | * @return {string[]} - An array of strings representing the lyrics. |
| 10 | 6 | */ |
| 11 | 7 | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 8 | // Edge Case: Invalid input types |
| 9 | if (typeof initialBottlesCount !== 'number' || typeof takeDownCount !== 'number') { |
| 10 | throw new Error('Both parameters must be numbers'); |
| 11 | } |
| 12 | |
| 13 | // Edge Case: Negative initial bottle count |
| 14 | if (initialBottlesCount < 0) { |
| 15 | throw new Error('Initial bottle count cannot be negative'); |
| 16 | } |
| 17 | |
| 18 | // Edge Case: Negative take down count |
| 19 | if (takeDownCount < 0) { |
| 20 | throw new Error('Take down count cannot be negative'); |
| 21 | } |
| 22 | |
| 23 | // Edge Case: takeDownCount is 0, return empty array |
| 24 | if (takeDownCount === 0) { |
| 25 | return []; |
| 26 | } |
| 27 | |
| 28 | const result = []; |
| 29 | |
| 30 | for (let i = 0; i < takeDownCount; i++) { |
| 31 | const currentBottles = initialBottlesCount - i; |
| 32 | |
| 33 | // Edge Case: Current bottles count goes below 0 |
| 34 | if (currentBottles < 0) { |
| 35 | break; |
| 36 | } |
| 37 | |
| 38 | // Handle the special case for 0 bottles |
| 39 | if (currentBottles === 0) { |
| 40 | result.push('No more bottles of beer on the wall, no more bottles of beer.'); |
| 41 | result.push('Go to the store and buy some more, 99 bottles of beer on the wall.'); |
| 42 | // Edge Case: When we reach 0 bottles, we don't need to continue |
| 43 | break; |
| 44 | } |
| 45 | |
| 46 | // Determine bottle text (singular vs plural) |
| 47 | const bottleText = currentBottles === 1 ? 'bottle' : 'bottles'; |
| 48 | const nextBottleCount = currentBottles - 1; |
| 49 | const nextBottleText = nextBottleCount === 1 ? 'bottle' : 'bottles'; |
| 50 | |
| 51 | // First line of the verse |
| 52 | result.push(`${currentBottles} ${bottleText} of beer on the wall, ${currentBottles} ${bottleText} of beer.`); |
| 53 | |
| 54 | // Second line of the verse |
| 55 | if (currentBottles === 1) { |
| 56 | result.push('Take it down and pass it around, no more bottles of beer on the wall.'); |
| 57 | } else if (nextBottleCount === 1) { |
| 58 | result.push(`Take one down and pass it around, ${nextBottleCount} ${nextBottleText} of beer on the wall.`); |
| 59 | } else if (nextBottleCount === 0) { |
| 60 | result.push('Take one down and pass it around, no more bottles of beer on the wall.'); |
| 61 | } else { |
| 62 | result.push(`Take one down and pass it around, ${nextBottleCount} ${nextBottleText} of beer on the wall.`); |
| 63 | } |
| 64 | |
| 65 | // Add empty line between verses except after the last verse |
| 66 | if (i < takeDownCount - 1 && currentBottles > 1) { |
| 67 | result.push(''); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return result; |
| 13 | 72 | }; |
| 73 | |
| 74 | // Handled Edge Cases: Invalid input types, negative initial bottle count, negative take down count, takeDownCount is 0, current bottles count goes below 0, when we reach 0 bottles |