| 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 numbers |
| 15 | if (initialBottlesCount < 0 || takeDownCount < 0) { |
| 16 | throw new Error('Parameters must be non-negative'); |
| 17 | } |
| 18 | |
| 19 | // Edge Case: takeDownCount is 0 |
| 20 | if (takeDownCount === 0) { |
| 21 | return []; |
| 22 | } |
| 23 | |
| 24 | const verses = []; |
| 25 | |
| 26 | for (let i = 0; i < takeDownCount; i++) { |
| 27 | const currentBottles = initialBottlesCount - i; |
| 28 | |
| 29 | // Edge Case: Going below 0 bottles |
| 30 | if (currentBottles < 0) { |
| 31 | break; |
| 32 | } |
| 33 | |
| 34 | let line1, line2; |
| 35 | |
| 36 | if (currentBottles === 0) { |
| 37 | line1 = 'No more bottles of beer on the wall, no more bottles of beer.'; |
| 38 | line2 = 'Go to the store and buy some more, 99 bottles of beer on the wall.'; |
| 39 | } else if (currentBottles === 1) { |
| 40 | line1 = '1 bottle of beer on the wall, 1 bottle of beer.'; |
| 41 | line2 = 'Take it down and pass it around, no more bottles of beer on the wall.'; |
| 42 | } else { |
| 43 | line1 = `${currentBottles} bottles of beer on the wall, ${currentBottles} bottles of beer.`; |
| 44 | const nextBottles = currentBottles - 1; |
| 45 | if (nextBottles === 1) { |
| 46 | line2 = `Take one down and pass it around, 1 bottle of beer on the wall.`; |
| 47 | } else if (nextBottles === 0) { |
| 48 | line2 = 'Take one down and pass it around, no more bottles of beer on the wall.'; |
| 49 | } else { |
| 50 | line2 = `Take one down and pass it around, ${nextBottles} bottles of beer on the wall.`; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | verses.push(line1); |
| 55 | verses.push(line2); |
| 56 | |
| 57 | // Add empty string for spacing between verses, except for the last verse |
| 58 | if (i < takeDownCount - 1) { |
| 59 | verses.push(''); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return verses; |
| 64 | |
| 65 | // Handled Edge Cases: Invalid input types, negative numbers, takeDownCount is 0, going below 0 bottles |
| 13 | 66 | }; |