| 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 | * Handles special cases for grammar and the final verses. |
| 4 | * |
| 5 | * @param {number} initialBottlesCount - The starting number of bottles (0-99) |
| 6 | * @param {number} takeDownCount - How many verses to recite (default: 1) |
| 7 | * @return {string[]} - Array of lyrics lines |
| 10 | 8 | */ |
| 11 | 9 | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 10 | // Edge Case: Invalid input types |
| 11 | if (typeof initialBottlesCount !== 'number' || typeof takeDownCount !== 'number') { |
| 12 | throw new Error('Both parameters must be numbers'); |
| 13 | } |
| 14 | |
| 15 | // Edge Case: Negative numbers |
| 16 | if (initialBottlesCount < 0 || takeDownCount < 0) { |
| 17 | throw new Error('Parameters must be non-negative'); |
| 18 | } |
| 19 | |
| 20 | // Edge Case: takeDownCount is 0 |
| 21 | if (takeDownCount === 0) { |
| 22 | return []; |
| 23 | } |
| 24 | |
| 25 | const result = []; |
| 26 | |
| 27 | for (let i = 0; i < takeDownCount; i++) { |
| 28 | const currentBottles = initialBottlesCount - i; |
| 29 | |
| 30 | // Edge Case: Going below 0 bottles |
| 31 | if (currentBottles < 0) { |
| 32 | break; |
| 33 | } |
| 34 | |
| 35 | // Handle the "no more" case (0 bottles) |
| 36 | if (currentBottles === 0) { |
| 37 | result.push('No more bottles of beer on the wall, no more bottles of beer.'); |
| 38 | result.push('Go to the store and buy some more, 99 bottles of beer on the wall.'); |
| 39 | } else { |
| 40 | // Handle singular vs plural for current bottle count |
| 41 | const currentBottleText = currentBottles === 1 ? '1 bottle' : `${currentBottles} bottles`; |
| 42 | |
| 43 | // Handle the last verse (1 bottle left) |
| 44 | if (currentBottles === 1) { |
| 45 | result.push(`${currentBottleText} of beer on the wall, ${currentBottleText} of beer.`); |
| 46 | result.push('Take it down and pass it around, no more bottles of beer on the wall.'); |
| 47 | } else { |
| 48 | // Standard verse |
| 49 | const nextBottles = currentBottles - 1; |
| 50 | const nextBottleText = nextBottles === 1 ? '1 bottle' : `${nextBottles} bottles`; |
| 51 | result.push(`${currentBottleText} of beer on the wall, ${currentBottleText} of beer.`); |
| 52 | result.push(`Take one down and pass it around, ${nextBottleText} of beer on the wall.`); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // Add empty line between verses (except after the last verse) |
| 57 | if (i < takeDownCount - 1 && currentBottles > 0) { |
| 58 | result.push(''); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return result; |
| 13 | 63 | }; |
| 64 | |
| 65 | // Handled Edge Cases: Invalid input types, Negative numbers, takeDownCount is 0, Going below 0 bottles |