| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | export class Song { |
| 7 | | verse() { |
| 8 | | throw new Error('Remove this line and implement the function'); |
| 7 | constructor() { |
| 8 | this.animals = [ |
| 9 | { |
| 10 | name: 'fly', |
| 11 | comment: null |
| 12 | }, |
| 13 | { |
| 14 | name: 'spider', |
| 15 | comment: 'It wriggled and jiggled and tickled inside her.', |
| 16 | catchPhrase: 'spider that wriggled and jiggled and tickled inside her' |
| 17 | }, |
| 18 | { |
| 19 | name: 'bird', |
| 20 | comment: 'How absurd to swallow a bird!' |
| 21 | }, |
| 22 | { |
| 23 | name: 'cat', |
| 24 | comment: 'Imagine that, to swallow a cat!' |
| 25 | }, |
| 26 | { |
| 27 | name: 'dog', |
| 28 | comment: 'What a hog, to swallow a dog!' |
| 29 | }, |
| 30 | { |
| 31 | name: 'goat', |
| 32 | comment: 'Just opened her throat and swallowed a goat!' |
| 33 | }, |
| 34 | { |
| 35 | name: 'cow', |
| 36 | comment: "I don't know how she swallowed a cow!" |
| 37 | }, |
| 38 | { |
| 39 | name: 'horse', |
| 40 | comment: "She's dead, of course!" |
| 41 | } |
| 42 | ]; |
| 9 | 43 | } |
| 10 | 44 | |
| 11 | | verses() { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 45 | verse(n) { |
| 46 | // Edge Case: Invalid verse number (less than 1 or greater than number of animals) |
| 47 | if (n < 1 || n > this.animals.length) { |
| 48 | throw new Error('Invalid verse number'); |
| 49 | } |
| 50 | |
| 51 | const animal = this.animals[n - 1]; |
| 52 | let lines = []; |
| 53 | |
| 54 | // Add first line |
| 55 | lines.push(`I know an old lady who swallowed a ${animal.name}.`); |
| 56 | |
| 57 | // Edge Case: Last animal (horse) has special ending |
| 58 | if (animal.name === 'horse') { |
| 59 | lines.push(animal.comment); |
| 60 | return lines.join('\n') + '\n'; |
| 61 | } |
| 62 | |
| 63 | // Add comment if exists |
| 64 | // Edge Case: First animal (fly) has no comment |
| 65 | if (animal.comment) { |
| 66 | lines.push(animal.comment); |
| 67 | } |
| 68 | |
| 69 | // Add cumulative lines |
| 70 | // Edge Case: First animal has no cumulative lines |
| 71 | for (let i = n - 1; i > 0; i--) { |
| 72 | const currentAnimal = this.animals[i]; |
| 73 | const previousAnimal = this.animals[i - 1]; |
| 74 | |
| 75 | let previousAnimalName = previousAnimal.name; |
| 76 | // Edge Case: Special phrasing for spider when it's the thing being caught |
| 77 | if (previousAnimal.catchPhrase) { |
| 78 | previousAnimalName = previousAnimal.catchPhrase; |
| 79 | } |
| 80 | |
| 81 | lines.push(`She swallowed the ${currentAnimal.name} to catch the ${previousAnimalName}.`); |
| 82 | } |
| 83 | |
| 84 | // Add final line |
| 85 | lines.push("I don't know why she swallowed the fly. Perhaps she'll die."); |
| 86 | |
| 87 | return lines.join('\n') + '\n'; |
| 88 | // Handled Edge Cases: Invalid verse number, last animal special case, first animal has no comment, first animal has no cumulative lines, special phrasing for spider |
| 89 | } |
| 90 | |
| 91 | verses(start, end) { |
| 92 | // Edge Case: Invalid range (start greater than end) |
| 93 | if (start > end) { |
| 94 | throw new Error('Start verse cannot be greater than end verse'); |
| 95 | } |
| 96 | |
| 97 | // Edge Case: Invalid verse numbers (less than 1 or greater than number of animals) |
| 98 | if (start < 1 || end > this.animals.length) { |
| 99 | throw new Error('Invalid verse number'); |
| 100 | } |
| 101 | |
| 102 | let result = []; |
| 103 | for (let i = start; i <= end; i++) { |
| 104 | // Remove trailing newline from each verse before joining |
| 105 | const verse = this.verse(i); |
| 106 | result.push(verse.slice(0, -1)); |
| 107 | } |
| 108 | |
| 109 | const joined = result.join('\n\n'); |
| 110 | return joined + '\n\n'; |
| 111 | // Handled Edge Cases: Invalid range, invalid verse numbers |
| 13 | 112 | } |
| 14 | 113 | } |