| 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 verses of the '99 Bottles of Beer' song. |
| 3 | * |
| 4 | * @param {number} initialBottlesCount - The starting number of bottles (0-99). |
| 5 | * @param {number} takeDownCount - The number of verses to generate (default: 1). |
| 6 | * @return {string[]} An array of strings representing the song verses. |
| 10 | 7 | */ |
| 11 | 8 | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 9 | // Edge Case: Invalid input types |
| 10 | if (typeof initialBottlesCount !== 'number' || typeof takeDownCount !== 'number') { |
| 11 | throw new Error('Both parameters must be numbers'); |
| 12 | } |
| 13 | |
| 14 | // Edge Case: Negative bottle count |
| 15 | if (initialBottlesCount < 0) { |
| 16 | throw new Error('Bottle count cannot be negative'); |
| 17 | } |
| 18 | |
| 19 | // Edge Case: Negative take down count |
| 20 | if (takeDownCount < 0) { |
| 21 | throw new Error('Take down count cannot be negative'); |
| 22 | } |
| 23 | |
| 24 | // Edge Case: takeDownCount is 0 |
| 25 | if (takeDownCount === 0) { |
| 26 | return []; |
| 27 | } |
| 28 | |
| 29 | const verses = []; |
| 30 | |
| 31 | for (let i = 0; i < takeDownCount; i++) { |
| 32 | const currentBottles = initialBottlesCount - i; |
| 33 | |
| 34 | // Edge Case: Current bottle count goes below 0 |
| 35 | if (currentBottles < 0) { |
| 36 | break; |
| 37 | } |
| 38 | |
| 39 | if (currentBottles === 0) { |
| 40 | verses.push('No more bottles of beer on the wall, no more bottles of beer.'); |
| 41 | verses.push('Go to the store and buy some more, 99 bottles of beer on the wall.'); |
| 42 | } else if (currentBottles === 1) { |
| 43 | verses.push('1 bottle of beer on the wall, 1 bottle of beer.'); |
| 44 | verses.push('Take it down and pass it around, no more bottles of beer on the wall.'); |
| 45 | } else if (currentBottles === 2) { |
| 46 | verses.push('2 bottles of beer on the wall, 2 bottles of beer.'); |
| 47 | verses.push('Take one down and pass it around, 1 bottle of beer on the wall.'); |
| 48 | } else { |
| 49 | verses.push(`${currentBottles} bottles of beer on the wall, ${currentBottles} bottles of beer.`); |
| 50 | verses.push(`Take one down and pass it around, ${currentBottles - 1} bottles of beer on the wall.`); |
| 51 | } |
| 52 | |
| 53 | // Add empty string for spacing between verses, except after the last verse |
| 54 | if (i < takeDownCount - 1) { |
| 55 | verses.push(''); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return verses; |
| 13 | 60 | }; |
| 61 | |
| 62 | // Handled Edge Cases: Invalid input types, negative bottle count, negative take down count, takeDownCount is 0, current bottle count goes below 0 |