| 9 | 9 | * @returns {string[]} |
| 10 | 10 | */ |
| 11 | 11 | static verse(verseNumber) { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 12 | // Edge Case: Invalid verse number (less than 1 or greater than 12) |
| 13 | if (verseNumber < 1 || verseNumber > 12) { |
| 14 | throw new Error('Verse number must be between 1 and 12'); |
| 15 | } |
| 16 | |
| 17 | const characters = [ |
| 18 | 'the house that Jack built.', |
| 19 | 'the malt', |
| 20 | 'the rat', |
| 21 | 'the cat', |
| 22 | 'the dog', |
| 23 | 'the cow with the crumpled horn', |
| 24 | 'the maiden all forlorn', |
| 25 | 'the man all tattered and torn', |
| 26 | 'the priest all shaven and shorn', |
| 27 | 'the rooster that crowed in the morn', |
| 28 | 'the farmer sowing his corn', |
| 29 | 'the horse and the hound and the horn' |
| 30 | ]; |
| 31 | |
| 32 | const verbs = [ |
| 33 | '', |
| 34 | 'lay in', |
| 35 | 'ate', |
| 36 | 'killed', |
| 37 | 'worried', |
| 38 | 'tossed', |
| 39 | 'milked', |
| 40 | 'kissed', |
| 41 | 'married', |
| 42 | 'woke', |
| 43 | 'kept', |
| 44 | 'belonged to' |
| 45 | ]; |
| 46 | |
| 47 | let result = ['This is ' + characters[verseNumber - 1]]; |
| 48 | |
| 49 | // Edge Case: First verse has no additional lines |
| 50 | if (verseNumber === 1) { |
| 51 | return result; |
| 52 | } |
| 53 | |
| 54 | // Build the recursive structure |
| 55 | for (let i = verseNumber - 2; i >= 0; i--) { |
| 56 | if (i === 0) { |
| 57 | // Base case: connect to the house |
| 58 | result.push('that ' + verbs[i + 1] + ' ' + characters[i]); |
| 59 | } else { |
| 60 | // Regular case: connect to previous character |
| 61 | result.push('that ' + verbs[i + 1] + ' ' + characters[i]); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return result; |
| 66 | // Handled Edge Cases: Invalid verse number, first verse special case |
| 13 | 67 | } |
| 14 | 68 | |
| 15 | 69 | /** |
| 18 | 72 | * @returns {string[]} |
| 19 | 73 | */ |
| 20 | 74 | static verses(start, end) { |
| 21 | | throw new Error('Remove this line and implement the function'); |
| 75 | // Edge Case: Invalid range (start or end out of bounds, or start > end) |
| 76 | if (start < 1 || end > 12 || start > end) { |
| 77 | throw new Error('Invalid range: start must be between 1 and 12, end must be between 1 and 12, and start must not exceed end'); |
| 78 | } |
| 79 | |
| 80 | let result = []; |
| 81 | for (let i = start; i <= end; i++) { |
| 82 | const verseLines = this.verse(i); |
| 83 | result.push(...verseLines); |
| 84 | // Edge Case: Add empty line between verses (except after the last verse) |
| 85 | if (i < end) { |
| 86 | result.push(''); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return result; |
| 91 | // Handled Edge Cases: Invalid range, proper spacing between verses |
| 22 | 92 | } |
| 23 | 93 | } |