| 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 - How many verses to generate (default: 1) |
| 5 | * @return {string[]} - 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'); |
| 13 | | }; |
| 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 numbers |
| 14 | if (initialBottlesCount < 0 || takeDownCount < 0) { |
| 15 | throw new Error('Parameters must be non-negative'); |
| 16 | } |
| 17 | |
| 18 | // Edge Case: takeDownCount of 0 |
| 19 | if (takeDownCount === 0) { |
| 20 | return []; |
| 21 | } |
| 22 | |
| 23 | const result = []; |
| 24 | |
| 25 | for (let i = 0; i < takeDownCount; i++) { |
| 26 | const currentBottles = initialBottlesCount - i; |
| 27 | |
| 28 | // Edge Case: Going below 0 bottles |
| 29 | if (currentBottles < 0) { |
| 30 | break; |
| 31 | } |
| 32 | |
| 33 | // Generate the first line |
| 34 | const bottleWord = currentBottles === 1 ? 'bottle' : 'bottles'; |
| 35 | const firstLine = `${currentBottles === 0 ? 'No more' : currentBottles} ${bottleWord} of beer on the wall, ${currentBottles === 0 ? 'no more' : currentBottles} ${bottleWord} of beer.`; |
| 36 | |
| 37 | // Generate the second line |
| 38 | let secondLine; |
| 39 | if (currentBottles === 0) { |
| 40 | secondLine = 'Go to the store and buy some more, 99 bottles of beer on the wall.'; |
| 41 | } else if (currentBottles === 1) { |
| 42 | secondLine = 'Take it down and pass it around, no more bottles of beer on the wall.'; |
| 43 | } else { |
| 44 | const nextBottles = currentBottles - 1; |
| 45 | const nextBottleWord = nextBottles === 1 ? 'bottle' : 'bottles'; |
| 46 | secondLine = `Take one down and pass it around, ${nextBottles === 0 ? 'no more' : nextBottles} ${nextBottleWord} of beer on the wall.`; |
| 47 | } |
| 48 | |
| 49 | result.push(firstLine); |
| 50 | result.push(secondLine); |
| 51 | |
| 52 | // Add empty line between verses except for the last verse |
| 53 | if (i < takeDownCount - 1) { |
| 54 | result.push(''); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | return result; |
| 59 | // Handled Edge Cases: Invalid input types, negative numbers, takeDownCount of 0, going below 0 bottles |
| 60 | }; |