Skip to content

Instantly share code, notes, and snippets.

@Hunta88
Last active August 22, 2022 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hunta88/f6e2da7c783fa931cc064672d8764cc6 to your computer and use it in GitHub Desktop.
Save Hunta88/f6e2da7c783fa931cc064672d8764cc6 to your computer and use it in GitHub Desktop.
What's incorrect about the findPopularDino() method in Park.js?
const Dinosaur = function (species, diet, guestsAttractedPerDay) {
this.species = species;
this.diet = diet;
this.guestsAttractedPerDay = guestsAttractedPerDay;
}
module.exports = Dinosaur;
const Dinosaur = require('./dinosaur');
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; this.dinosaurCollection.length; i++){
array.push(this.dinosaurCollection[i].guestsAttractedPerDay);
}
let indexOfMostPopularDino = array.indexOf(Math.max(...array))
mostPopularDino = this.dinosaurCollection[indexOfMostPopularDino]
return mostPopularDino;
}
module.exports = Park;
const assert = require('assert');
const Park = require('../models/park.js');
const Dinosaur = require('../models/dinosaur.js');
describe('Park', function() {
let dino1;
let dino2;
let dino3;
dino1 = new Dinosaur('t-rex', 'carnivore', 50);
dino2 = new Dinosaur('triseratops', 'herbivore', 50);
dino3 = new Dinosaur('velosiraptor', 'carnivore', 50);
let dinos = [dino1, dino2, dino3];
let park;
beforeEach(function () {
park = new Park("Jurassic Park", 20, dinos);
});
it('should have a name',function(){
const actual = park.name;
assert.strictEqual(actual, 'Jurassic Park')
}
);
it('should have a ticket price', function(){
const actual = park.ticketPrice;
assert.strictEqual(actual, 20)
}
);
it('should have a collection of dinosaurs', function (){
const actual = park.dinosaurCollection.length
assert.strictEqual(actual, 3)
});
it('should be able to add a dinosaur to its collection', function(){
let newDino = Dinosaur ('Stegasoraus', 'herbivore', 60);
park.addDinoaour(newDino);
let actual = park.dinosaurCollection.length
assert.strictEqual(actual, 4)
});
it('should be able to remove a dinosaur from its collection', function(){
park.removeDinosaur(0);
let actual = park.dinosaurCollection.length
assert.strictEqual(actual, 3)
});
it('should be able to find the dinosaur that attracts the most visitors', function(){
let actual = park.findMostPopular()
assert.strictEqual(actual.species, 'Stegasoraus')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment