| 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 the lyrics for the beer song starting from a given number of bottles. |
| 3 | * @param {number} initialBottlesCount - The starting number of bottles (0-99). |
| 4 | * @param {number} takeDownCount - The number of verses to generate (default is 1). |
| 5 | * @return {string[]} - An array of strings representing the lyrics. |
| 10 | 6 | */ |
| 11 | 7 | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 13 | | }; |
| 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 is 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: Current bottles count goes below 0 |
| 29 | if (currentBottles < 0) { |
| 30 | break; |
| 31 | } |
| 32 | |
| 33 | // Handle the first line of the verse |
| 34 | let firstLine; |
| 35 | if (currentBottles === 0) { |
| 36 | firstLine = 'No more bottles of beer on the wall, no more bottles of beer.'; |
| 37 | } else if (currentBottles === 1) { |
| 38 | firstLine = '1 bottle of beer on the wall, 1 bottle of beer.'; |
| 39 | } else { |
| 40 | firstLine = `${currentBottles} bottles of beer on the wall, ${currentBottles} bottles of beer.`; |
| 41 | } |
| 42 | |
| 43 | // Handle the second line of the verse |
| 44 | let secondLine; |
| 45 | const nextBottles = currentBottles - 1; |
| 46 | |
| 47 | if (currentBottles === 0) { |
| 48 | secondLine = 'Go to the store and buy some more, 99 bottles of beer on the wall.'; |
| 49 | } else if (currentBottles === 1) { |
| 50 | secondLine = 'Take it down and pass it around, no more bottles of beer on the wall.'; |
| 51 | } else if (nextBottles === 1) { |
| 52 | secondLine = 'Take one down and pass it around, 1 bottle of beer on the wall.'; |
| 53 | } else { |
| 54 | secondLine = `Take one down and pass it around, ${nextBottles} bottles of beer on the wall.`; |
| 55 | } |
| 56 | |
| 57 | result.push(firstLine); |
| 58 | result.push(secondLine); |
| 59 | |
| 60 | // Add an empty line after each verse except the last one |
| 61 | if (i < takeDownCount - 1) { |
| 62 | result.push(''); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return result; |
| 67 | |
| 68 | // Handled Edge Cases: Invalid input types, negative numbers, takeDownCount is 0, current bottles count goes below 0 |
| 69 | }; |