Object method not returning correctly?

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.

Error message:

1) Park
       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)

Note: I am also trying TDD with mocha.

The for-loop in the guestsAttractedPerDay method should have
i < this.dinosaurCollection.length
It seems to work otherwise.

const Dinosaur = function (species, diet, guestsAttractedPerDay) { this.species = species; this.diet = diet; this.guestsAttractedPerDay = guestsAttractedPerDay; } const Park = function (name, ticketPrice, dinosaurCollection){ this.name = name; this.ticketPrice = ticketPrice; this.dinosaurCollection = dinosaurCollection; } Park.prototype.addDinoaour = function(dinosaur){ this.dinosaurCollection.push(dinosaur); } Park.prototype.removeDinosaur = function(indexnumber){ this.dinosaurCollection.splice(indexnumber,1); } Park.prototype.findMostPopular = function(){ let mostPopularDino; let array = []; for(let i = 0; i < this.dinosaurCollection.length; i++){ array.push(this.dinosaurCollection[i].guestsAttractedPerDay); } let indexOfMostPopularDino = array.indexOf(Math.max(...array)) mostPopularDino = this.dinosaurCollection[indexOfMostPopularDino] return mostPopularDino; } let dino1 = new Dinosaur('t-rex', 'carnivore', 200); let dino2 = new Dinosaur('triseratops', 'herbivore', 150); let dino3 = new Dinosaur('velosiraptor', 'carnivore', 250); let dinos = [dino1, dino2, dino3]; let park = new Park("Jurassic Park", 20, dinos); console.log("most popular: "); console.log(park.findMostPopular().species);
1 Like

Looks like your for loop suggestion should work but still getting the same error:

 TypeError: Cannot read properties of undefined (reading 'guestsAttractedPerDay')
  at Park.findMostPopular (models/park.js:22:47)

A few passing remarks:

  • 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);

park.addDinoaour(newDino);

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);

park.addDinoaour(newDino);

Have a read of the description section of documentation for new to see why the new operator is necessary.

I’m shaking my head realizing that I didn’t use new :upside_down_face:

Confirm that was the issue. Thanks for the review!

1 Like