| 1 | /* eslint-disable no-undef */ |
| 1 | 2 | // |
| 2 | 3 | // This is only a SKELETON file for the 'Beer Song' exercise. It's been provided as a |
| 3 | 4 | // convenience to get you started writing code faster. |
| 4 | 5 | // |
| 5 | 6 | |
| 6 | 7 | /** |
| 7 | | * @param {number} initialBottlesCount |
| 8 | | * @param {number} takeDownCount |
| 9 | | * @return {string[]} |
| 8 | * Generates the lyrics for a range of verses in the 99 Bottles of Beer song. |
| 9 | * |
| 10 | * @param {number} initialBottlesCount - The starting number of bottles (0-99) |
| 11 | * @param {number} takeDownCount - The number of verses to generate (default: 1) |
| 12 | * @return {string[]} - Array of strings representing the lyrics |
| 10 | 13 | */ |
| 11 | 14 | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 15 | // Edge Case: Handle invalid inputs |
| 16 | if (typeof initialBottlesCount !== 'number' || initialBottlesCount < 0 || initialBottlesCount > 99) { |
| 17 | throw new Error('initialBottlesCount must be between 0 and 99'); |
| 18 | } |
| 19 | if (typeof takeDownCount !== 'number' || takeDownCount < 0) { |
| 20 | throw new Error('takeDownCount must be a non-negative number'); |
| 21 | } |
| 22 | |
| 23 | const lines = []; |
| 24 | |
| 25 | // Edge Case: Handle when takeDownCount is 0 |
| 26 | if (takeDownCount === 0) { |
| 27 | return []; |
| 28 | } |
| 29 | |
| 30 | for (let i = 0; i < takeDownCount; i++) { |
| 31 | const currentBottles = initialBottlesCount - i; |
| 32 | |
| 33 | // Edge Case: Prevent negative bottle counts |
| 34 | if (currentBottles < 0) { |
| 35 | break; |
| 36 | } |
| 37 | |
| 38 | // Add empty line between verses (except for the first verse) |
| 39 | if (i > 0) { |
| 40 | lines.push(''); |
| 41 | } |
| 42 | |
| 43 | // Add the two lines of the verse |
| 44 | const verseLines = createVerse(currentBottles); |
| 45 | lines.push(...verseLines); |
| 46 | } |
| 47 | |
| 48 | return lines; |
| 49 | }; |
| 50 | |
| 51 | /** |
| 52 | * Creates a single verse of the 99 Bottles of Beer song. |
| 53 | * |
| 54 | * @param {number} bottles - The number of bottles for this verse |
| 55 | * @return {string[]} - Array of two strings representing the two lines of the verse |
| 56 | */ |
| 57 | const createVerse = (bottles) => { |
| 58 | // Edge Case: Handle special case for 0 bottles |
| 59 | if (bottles === 0) { |
| 60 | return [ |
| 61 | 'No more bottles of beer on the wall, no more bottles of beer.', |
| 62 | 'Go to the store and buy some more, 99 bottles of beer on the wall.' |
| 63 | ]; |
| 64 | } |
| 65 | |
| 66 | // Edge Case: Handle special case for 1 bottle |
| 67 | if (bottles === 1) { |
| 68 | return [ |
| 69 | '1 bottle of beer on the wall, 1 bottle of beer.', |
| 70 | 'Take it down and pass it around, no more bottles of beer on the wall.' |
| 71 | ]; |
| 72 | } |
| 73 | |
| 74 | // Edge Case: Handle special case for 2 bottles |
| 75 | if (bottles === 2) { |
| 76 | return [ |
| 77 | '2 bottles of beer on the wall, 2 bottles of beer.', |
| 78 | 'Take one down and pass it around, 1 bottle of beer on the wall.' |
| 79 | ]; |
| 80 | } |
| 81 | |
| 82 | // General case for 3 or more bottles |
| 83 | return [ |
| 84 | `${bottles} bottles of beer on the wall, ${bottles} bottles of beer.`, |
| 85 | `Take one down and pass it around, ${bottles - 1} bottles of beer on the wall.` |
| 86 | ]; |
| 13 | 87 | }; |
| 88 | |
| 89 | // Handled Edge Cases: |
| 90 | // - Invalid initialBottlesCount values (< 0 or > 99) |
| 91 | // - Invalid takeDownCount values (< 0) |
| 92 | // - takeDownCount = 0 |
| 93 | // - Negative bottle counts during iteration |
| 94 | // - Special case for 0 bottles |
| 95 | // - Special case for 1 bottle |
| 96 | // - Special case for 2 bottles |