Coding task · JavaScript and TypeScript course · Module 2: Functions, Arrays and Objects

Practice: Dinosaur Factory

Variables declared inside the factory function are private, and the returned methods can reach them thanks to the closure. You return a copy of an array with [...array].

Task

Write a function createDinoTracker(species) that keeps a private counter sightings and an array history in a closure and returns an object with the methods: reportSighting(location) (increases the counter and adds the location to the history), getSightings() (returns the number of sightings) and getHistory() (returns a copy of the history). Also write createEnclosure(name, maxCapacity) with a private array residents and the methods: addDinosaur(dinoName) (adds the dinosaur and returns true, or returns false when there is no room), removeDinosaur(dinoName), getPopulation() (returns a copy of the array) and getStatus() (returns the text 'name: X/maxCapacity', e.g. 'Paddock T-Rex: 1/2').