| 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 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 takeDownCount |
| 15 | if (takeDownCount < 0) { |
| 16 | throw new Error('takeDownCount cannot be negative'); |
| 17 | } |
| 18 | |
| 19 | // Edge Case: Initial count out of range |
| 20 | if (initialBottlesCount < 0 || initialBottlesCount > 99) { |
| 21 | throw new Error('initialBottlesCount must be between 0 and 99'); |
| 22 | } |
| 23 | |
| 24 | // Edge Case: Not enough bottles to take down |
| 25 | // Allow taking down bottles even if it goes to zero or below, as we have special verses for those cases |
| 26 | if (initialBottlesCount < 0) { |
| 27 | throw new Error('Cannot start with negative bottles'); |
| 28 | } |
| 29 | |
| 30 | const verses = []; |
| 31 | |
| 32 | for (let i = 0; i < takeDownCount; i++) { |
| 33 | // Handle circular counting: wrap around from 0 to 99, then continue down |
| 34 | let currentBottles = initialBottlesCount - i; |
| 35 | if (currentBottles < 0) { |
| 36 | currentBottles = 99 + currentBottles; // Wrap around: -1 -> 98, -2 -> 97, etc. |
| 37 | } |
| 38 | // Handle wrap-around for next bottles |
| 39 | let nextBottles = currentBottles - 1; |
| 40 | if (nextBottles < 0) { |
| 41 | nextBottles = 99; // After 0, go to 99 |
| 42 | } |
| 43 | |
| 44 | // Generate the first line |
| 45 | const firstLine = `${formatBottles(currentBottles)} of beer on the wall, ${formatBottles(currentBottles).toLowerCase()} of beer.`; |
| 46 | |
| 47 | // Generate the second line based on the number of bottles |
| 48 | let secondLine; |
| 49 | if (currentBottles === 1) { |
| 50 | secondLine = 'Take it down and pass it around, no more bottles of beer on the wall.'; |
| 51 | } else if (currentBottles === 0) { |
| 52 | secondLine = 'Go to the store and buy some more, 99 bottles of beer on the wall.'; |
| 53 | } else { |
| 54 | secondLine = `Take one down and pass it around, ${formatBottles(nextBottles).toLowerCase()} of beer on the wall.`; |
| 55 | } |
| 56 | |
| 57 | verses.push(firstLine); |
| 58 | verses.push(secondLine); |
| 59 | |
| 60 | // Add an empty line between verses except after the last verse |
| 61 | if (i < takeDownCount - 1) { |
| 62 | verses.push(''); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return verses; |
| 67 | |
| 68 | // Helper function to format the bottle count correctly |
| 69 | function formatBottles(count) { |
| 70 | if (count === 0) { |
| 71 | return 'No more bottles'; |
| 72 | } else if (count === 1) { |
| 73 | return '1 bottle'; |
| 74 | } else { |
| 75 | return `${count} bottles`; |
| 76 | } |
| 77 | } |
| 13 | 78 | }; |
| 79 | |
| 80 | // Handled Edge Cases: Invalid input types, negative takeDownCount, initial count out of range, not enough bottles to take down |