| 9 | 9 | * @return {string[]} |
| 10 | 10 | */ |
| 11 | 11 | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 12 | // Edge Case: Invalid input types or negative values |
| 13 | if (typeof initialBottlesCount !== 'number' || typeof takeDownCount !== 'number' || initialBottlesCount < 0 || takeDownCount < 0) { |
| 14 | throw new Error('Invalid input: counts must be non-negative numbers'); |
| 15 | } |
| 16 | |
| 17 | // Edge Case: takeDownCount is 0, return empty array |
| 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: currentBottles is less than 0 |
| 29 | if (currentBottles < 0) { |
| 30 | break; |
| 31 | } |
| 32 | |
| 33 | let currentBottleText = ''; |
| 34 | let nextBottleText = ''; |
| 35 | let actionText = ''; |
| 36 | |
| 37 | // Handle special cases for bottle counts |
| 38 | if (currentBottles === 0) { |
| 39 | // Edge Case: No more bottles |
| 40 | currentBottleText = 'No more bottles of beer'; |
| 41 | actionText = 'Go to the store and buy some more'; |
| 42 | nextBottleText = '99 bottles of beer'; |
| 43 | } else if (currentBottles === 1) { |
| 44 | // Edge Case: One bottle |
| 45 | currentBottleText = '1 bottle of beer'; |
| 46 | actionText = 'Take it down and pass it around'; |
| 47 | nextBottleText = 'no more bottles of beer'; |
| 48 | } else { |
| 49 | // Edge Case: Multiple bottles |
| 50 | currentBottleText = `${currentBottles} bottles of beer`; |
| 51 | actionText = 'Take one down and pass it around'; |
| 52 | if (nextBottles === 1) { |
| 53 | nextBottleText = '1 bottle of beer'; |
| 54 | } else if (nextBottles === 0) { |
| 55 | nextBottleText = 'no more bottles of beer'; |
| 56 | } else { |
| 57 | nextBottleText = `${nextBottles} bottles of beer`; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Edge Case: Last verse when reaching 0 bottles |
| 62 | if (currentBottles === 0) { |
| 63 | verses.push(`${currentBottleText} on the wall, ${currentBottleText.toLowerCase()}.`); |
| 64 | verses.push(`${actionText}, ${nextBottleText} on the wall.`); |
| 65 | } else { |
| 66 | verses.push(`${currentBottleText} on the wall, ${currentBottleText.toLowerCase()}.`); |
| 67 | verses.push(`${actionText}, ${nextBottleText} on the wall.`); |
| 68 | } |
| 69 | |
| 70 | // Edge Case: Add empty string after each verse except the last one |
| 71 | if (i < takeDownCount - 1) { |
| 72 | verses.push(''); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return verses; |
| 13 | 77 | }; |
| 78 | |
| 79 | // Handled Edge Cases: Invalid input types or negative values, takeDownCount is 0, currentBottles is less than 0, No more bottles, One bottle, Multiple bottles, Last verse when reaching 0 bottles, Add empty string after each verse except the last one |