| 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 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 takeDownCount |
| 14 | if (takeDownCount < 0) { |
| 15 | throw new Error('takeDownCount cannot be negative'); |
| 16 | } |
| 17 | |
| 18 | // Edge Case: initialBottlesCount out of range |
| 19 | if (initialBottlesCount < 0 || initialBottlesCount > 99) { |
| 20 | throw new Error('initialBottlesCount must be between 0 and 99'); |
| 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 | // Handle special cases for grammar |
| 34 | const bottleWord = currentBottles === 1 ? 'bottle' : 'bottles'; |
| 35 | const nextBottles = currentBottles - 1; |
| 36 | |
| 37 | if (currentBottles > 1) { |
| 38 | result.push(`${currentBottles} ${bottleWord} of beer on the wall, ${currentBottles} ${bottleWord} of beer.`); |
| 39 | if (nextBottles === 1) { |
| 40 | result.push('Take one down and pass it around, 1 bottle of beer on the wall.'); |
| 41 | } else if (nextBottles === 0) { |
| 42 | result.push('Take one down and pass it around, no more bottles of beer on the wall.'); |
| 43 | } else { |
| 44 | result.push(`Take one down and pass it around, ${nextBottles} bottles of beer on the wall.`); |
| 45 | } |
| 46 | } else if (currentBottles === 1) { |
| 47 | result.push('1 bottle of beer on the wall, 1 bottle of beer.'); |
| 48 | result.push('Take it down and pass it around, no more bottles of beer on the wall.'); |
| 49 | } else { // currentBottles === 0 |
| 50 | result.push('No more bottles of beer on the wall, no more bottles of beer.'); |
| 51 | result.push('Go to the store and buy some more, 99 bottles of beer on the wall.'); |
| 52 | } |
| 53 | |
| 54 | // Add empty line between verses, but not after the last one |
| 55 | if (i < takeDownCount - 1) { |
| 56 | result.push(''); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return result; |
| 13 | 61 | }; |
| 62 | |
| 63 | // Handled Edge Cases: Invalid input types, negative takeDownCount, initialBottlesCount out of range, going below 0 bottles |