I’m stuck on this exercise about using methods. In the Park findMostPopular method, I’m hoping to return the object with the highest ‘guestsAttractedPerDay’ property from an array of objects.
I’ve only been able to get back an empty object. Any suggestions? I’m relatively new to JS.
should be able to find the dinosaur that attracts the most visitors:
TypeError: Cannot read properties of undefined (reading 'guestsAttractedPerDay')
at Park.findMostPopular (models/park.js:22:47)
at Context.<anonymous> (specs/park_spec.js:58:24)
at processImmediate (node:internal/timers:466:21)
As noted by janbazant1107978602, you need to amend your loop condition (which based on your last post you seem to have done).
Also, you have Park.prototype.addDinoaour. Later you invoke it as park.addDinoaour(newDino);. So it works perfectly fine. Just mentioning it in case you inadvertently wrote Dinoaour instead of Dinosaur. If intentional, then it’s fine.
You are getting the error message just because of three letters. The magic word you omitted is new
In your assertions, you wote:
let newDino = Dinosaur ('Stegasoraus', 'herbivore', 60);
You later check if dinosaurCollection’s length has increased to 4 which passes without issue. But if you console.log(park.dinosaurCollection) , you will see that the dinosaur you added shows up as undefined.
If you just add three letters to your code, it works fine:
let newDino = new Dinosaur ('Stegasoraus', 'herbivore', 60);