| 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[]} |
| 10 | | */ |
| 11 | | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 1 | const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 2 | // Edge Case: Invalid input types |
| 3 | if (typeof initialBottlesCount !== 'number' || typeof takeDownCount !== 'number') { |
| 4 | throw new Error('Invalid input: Both parameters must be numbers'); |
| 5 | } |
| 6 | |
| 7 | // Edge Case: Negative initial bottles count |
| 8 | if (initialBottlesCount < 0) { |
| 9 | throw new Error('Invalid input: initialBottlesCount cannot be negative'); |
| 10 | } |
| 11 | |
| 12 | // Edge Case: Negative take down count |
| 13 | if (takeDownCount < 0) { |
| 14 | throw new Error('Invalid input: takeDownCount cannot be negative'); |
| 15 | } |
| 16 | |
| 17 | // Edge Case: takeDownCount is 0 |
| 18 | if (takeDownCount === 0) { |
| 19 | return []; |
| 20 | } |
| 21 | |
| 22 | const verses = []; |
| 23 | |
| 24 | for (let i = 0; i < takeDownCount; i++) { |
| 25 | const currentBottles = initialBottlesCount - i; |
| 26 | const nextBottles = currentBottles - 1; |
| 27 | |
| 28 | // Edge Case: Current bottles count is less than 0 |
| 29 | if (currentBottles < 0) { |
| 30 | break; |
| 31 | } |
| 32 | |
| 33 | let currentBottleText = ''; |
| 34 | let nextBottleText = ''; |
| 35 | let actionText = ''; |
| 36 | |
| 37 | if (currentBottles === 0) { |
| 38 | // Edge Case: No more bottles |
| 39 | currentBottleText = 'No more bottles of beer'; |
| 40 | actionText = 'Go to the store and buy some more'; |
| 41 | nextBottleText = '99 bottles of beer'; |
| 42 | } else if (currentBottles === 1) { |
| 43 | // Edge Case: One bottle |
| 44 | currentBottleText = '1 bottle of beer'; |
| 45 | actionText = 'Take it down and pass it around'; |
| 46 | nextBottleText = 'no more bottles of beer'; |
| 47 | } else { |
| 48 | // Normal case: Multiple bottles |
| 49 | currentBottleText = `${currentBottles} bottles of beer`; |
| 50 | actionText = 'Take one down and pass it around'; |
| 51 | if (nextBottles === 1) { |
| 52 | nextBottleText = '1 bottle of beer'; |
| 53 | } else if (nextBottles === 0) { |
| 54 | nextBottleText = 'no more bottles of beer'; |
| 55 | } else { |
| 56 | nextBottleText = `${nextBottles} bottles of beer`; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | verses.push(`${currentBottleText} on the wall, ${currentBottleText.toLowerCase()}.`); |
| 61 | verses.push(`${actionText}, ${nextBottleText} on the wall.`); |
| 62 | |
| 63 | // Add empty string for spacing between verses except for the last verse |
| 64 | if (i < takeDownCount - 1) { |
| 65 | verses.push(''); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return verses; |
| 13 | 70 | }; |
| 71 | |
| 72 | export { recite }; |
| 73 | |
| 74 | // Handled Edge Cases: Invalid input types, Negative initial bottles count, Negative take down count, takeDownCount is 0, Current bottles count is less than 0, No more bottles, One bottle |