| 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: Invalid input types |
| 16 | if (typeof initialBottlesCount !== 'number' || typeof takeDownCount !== 'number') { |
| 17 | throw new Error('Both parameters must be numbers'); |
| 18 | } |
| 19 | |
| 20 | // Edge Case: Negative values |
| 21 | if (initialBottlesCount < 0 || takeDownCount < 0) { |
| 22 | throw new Error('Parameters must be non-negative'); |
| 23 | } |
| 24 | |
| 25 | // Edge Case: takeDownCount is 0 |
| 26 | if (takeDownCount === 0) { |
| 27 | return []; |
| 28 | } |
| 29 | |
| 30 | const verses = []; |
| 31 | |
| 32 | for (let i = 0; i < takeDownCount; i++) { |
| 33 | const currentBottles = initialBottlesCount - i; |
| 34 | |
| 35 | // Edge Case: Current bottles count goes below 0 |
| 36 | if (currentBottles < 0) { |
| 37 | break; |
| 38 | } |
| 39 | |
| 40 | verses.push(...generateVerse(currentBottles)); |
| 41 | |
| 42 | // Add empty string for spacing between verses, except for the last verse |
| 43 | if (i < takeDownCount - 1 && currentBottles > 0) { |
| 44 | verses.push(''); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | return verses; |
| 13 | 49 | }; |
| 50 | |
| 51 | /** |
| 52 | * Generates 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 strings representing the verse |
| 56 | */ |
| 57 | const generateVerse = (bottles) => { |
| 58 | // Edge Case: No more 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: One 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: Two 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 | ]; |
| 87 | }; |
| 88 | |
| 89 | // Handled Edge Cases: |
| 90 | // - Invalid input types |
| 91 | // - Negative values |
| 92 | // - takeDownCount is 0 |
| 93 | // - Current bottles count goes below 0 |
| 94 | // - No more bottles (0) |
| 95 | // - One bottle (1) |
| 96 | // - Two bottles (2) |
| 97 | // - General case (3 or more bottles) |