| 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 | * Recites the lyrics to '99 Bottles of Beer on the Wall' |
| 3 | * @param {number} initialBottlesCount - The starting number of bottles (0-99) |
| 4 | * @param {number} takeDownCount - How many verses to recite (default: 1) |
| 5 | * @return {string[]} - Array of lyrics lines |
| 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 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 | // Handle special cases for grammar |
| 34 | const currentBottleWord = currentBottles === 1 ? 'bottle' : 'bottles'; |
| 35 | const nextBottles = currentBottles - 1; |
| 36 | const nextBottleWord = nextBottles === 1 ? 'bottle' : 'bottles'; |
| 37 | |
| 38 | if (currentBottles > 1) { |
| 39 | result.push(`${currentBottles} ${currentBottleWord} of beer on the wall, ${currentBottles} ${currentBottleWord} of beer.`); |
| 40 | if (nextBottles > 1) { |
| 41 | result.push(`Take one down and pass it around, ${nextBottles} ${nextBottleWord} of beer on the wall.`); |
| 42 | } else if (nextBottles === 1) { |
| 43 | result.push(`Take one down and pass it around, ${nextBottles} ${nextBottleWord} of beer on the wall.`); |
| 44 | } else { |
| 45 | result.push('Take one down and pass it around, no more bottles of beer on the wall.'); |
| 46 | } |
| 47 | } else if (currentBottles === 1) { |
| 48 | result.push(`${currentBottles} ${currentBottleWord} of beer on the wall, ${currentBottles} ${currentBottleWord} of beer.`); |
| 49 | result.push('Take it down and pass it around, no more bottles of beer on the wall.'); |
| 50 | } else { // currentBottles === 0 |
| 51 | result.push('No more bottles of beer on the wall, no more bottles of beer.'); |
| 52 | result.push('Go to the store and buy some more, 99 bottles of beer on the wall.'); |
| 53 | } |
| 54 | |
| 55 | // Add empty line between verses except after the last verse |
| 56 | if (i < takeDownCount - 1 && currentBottles > 0) { |
| 57 | result.push(''); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return result; |
| 13 | 62 | }; |
| 63 | |
| 64 | // Handled Edge Cases: Invalid input types, negative numbers, takeDownCount of 0, going below 0 bottles |