| 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 numbers |
| 14 | if (initialBottlesCount < 0 || takeDownCount < 0) { |
| 15 | throw new Error('Parameters must be non-negative'); |
| 16 | } |
| 17 | |
| 18 | // Edge Case: takeDownCount is 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: Current bottles count goes below 0 |
| 29 | if (currentBottles < 0) { |
| 30 | break; |
| 31 | } |
| 32 | |
| 33 | // Handle the special "no more bottles" verse |
| 34 | if (currentBottles === 0) { |
| 35 | result.push('No more bottles of beer on the wall, no more bottles of beer.'); |
| 36 | result.push('Go to the store and buy some more, 99 bottles of beer on the wall.'); |
| 37 | // Edge Case: When we reach the final verse, we don't add an empty line |
| 38 | if (i < takeDownCount - 1) { |
| 39 | result.push(''); |
| 40 | } |
| 41 | continue; |
| 42 | } |
| 43 | |
| 44 | // Determine bottle text for current verse |
| 45 | const bottleText = currentBottles === 1 ? '1 bottle' : `${currentBottles} bottles`; |
| 46 | |
| 47 | // First line of the verse |
| 48 | result.push(`${bottleText} of beer on the wall, ${bottleText} of beer.`); |
| 49 | |
| 50 | // Determine next bottle count and text |
| 51 | const nextBottles = currentBottles - 1; |
| 52 | let nextBottleText; |
| 53 | |
| 54 | if (nextBottles === 0) { |
| 55 | nextBottleText = 'no more bottles'; |
| 56 | } else if (nextBottles === 1) { |
| 57 | nextBottleText = '1 bottle'; |
| 58 | } else { |
| 59 | nextBottleText = `${nextBottles} bottles`; |
| 60 | } |
| 61 | |
| 62 | // Determine action text |
| 63 | const actionText = currentBottles === 1 ? 'Take it down and pass it around' : 'Take one down and pass it around'; |
| 64 | |
| 65 | // Second line of the verse |
| 66 | result.push(`${actionText}, ${nextBottleText} of beer on the wall.`); |
| 67 | |
| 68 | // Edge Case: Add empty line between verses except after the last verse |
| 69 | if (i < takeDownCount - 1) { |
| 70 | result.push(''); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return result; |
| 13 | 75 | }; |
| 76 | |
| 77 | // Handled Edge Cases: Invalid input types, negative numbers, takeDownCount is 0, current bottles count goes below 0 |