Coding Challenge
CODING CHALLENGE
Suppose that you're working in a small town administration, and you're in charge of two town elements:
1. Parks
2. Streets
It's a very small town, so right now there are only 3 parks and 4 streets. All parks and streets have a name and a build year.
At an end-of-year meeting, your boss wants a final report with the following:
1. Tree density of each park in the town (forumla: number of trees/park area)
2. Average age of each town's park (forumla: sum of all ages/number of parks)
3. The name of the park that has more than 1000 trees
4. Total and average length of the town's streets
5. Size classification of all streets: tiny/small/normal/big/huge. If the size is unknown, the default is normal
All the report data should be printed to the console.
HINT: Use some of the ES6 features: classes, subclasses, template strings, default parameters,
maps, arrow functions, destructuring, etc.
Solution
class Element { Superclass, Element
constructor(name, buildYear) {
this.name = name;
this.buildYear = buildYear;
}
}
class Park extends Element { Subclass of Park of superclass Element
constructor(name, buildYear, area, numTrees) {
super(name, buildYear); Imports this data from the superclass
this.area = area; This is km2
this.numTrees = numTrees;
}
treeDensity() { Calculates treeDensity
const density = this.numTrees / this.area;
console.log(`${this.name} has a tree density of ${density} trees per square km.`); Just logging it to the console
}
}
class Street extends Element { Subclass Street of superclass Element
constructor(name, buildYear, length, size = 3) { Default is size = 3 because this is the normal size, so if we don't define it when creating a new Street, this size is going to be used
super(name, buildYear);
this.length = length;
this.size = size;
}
classifyStreet () {
const classification = new Map(); Remember that map is like an object
classification.set(1, 'tiny');
classification.set(2, 'small');
classification.set(3, 'normal');
classification.set(4, 'big');
classification.set(5, 'huge');
console.log(`${this.name}, build in ${this.buildYear}, is a ${classification.get(this.size)} street.`); Use get method to use data from the map
}
}
const allParks = [new Park('Green Park', 1987, 0.2, 215), Array containing all parks and it's informations
new Park('National Park', 1894, 2.9, 3541),
new Park('Oak Park', 1953, 0.4, 949)];
const allStreets = [new Street('Ocean Avenue', 1999, 1.1, 4),
new Street('Evergreen Street', 2008, 2.7, 2),
new Street('4th Street', 2015, 0.8),
new Street('Sunset Boulevard', 1982, 2.5, 5)];
function calc(arr) { Calculates something based on the received array
const sum = arr.reduce((prev, cur, index) => prev + cur, 0); The reduce method basically loops through an array to accumulate all values into a single value (ES5 method)
prev = previous, cur = current, index = index, => (what we want to return) is the prev + cur, 0 (0 = inicial number of the sum)
Example: [3, 5, 6] = 0 + 3 = 3, next iteration: 3+5 = 8 (being 3 the previous and 5 the cur element), next iteration: 8+6 = 14
return [sum, sum / arr.length]; Destruction, returning two things, first the sum and then sum/arr.length, which is the average. This destructuring will be received on Average Age
}
function reportParks(p) { Function that writes the report
console.log('-----PARKS REPORT-----');
// Density
p.forEach(el => el.treeDensity()); forEach method to go through all the parks calling the treeDensity method on each of them, using the current allParks array informations
// Average age
const ages = p.map(el => new Date().getFullYear() - el.buildYear); Current build age: present year - buildYear
const [totalAge, avgAge] = calc(ages); totalAge is the sum of cuntion calc(arr), avgAge is the sum / arr.length
console.log(`Our ${p.length} parks have an average of ${avgAge} years.`);
// Which park has more than 1000 trees
const i = p.map(el => el.numTrees).findIndex(el => el >= 1000); Find the index of the element greater or equal to 1000 tress
console.log(`${p[i].name} has more than 1000 trees.`); Park name has more than 1000 trees
}
function reportStreets(s) {
console.log('-----STREETS REPORT-----');
//Total and average length of the town's streets
const [totalLength, avgLength] = calc(s.map(el => el.length)); s as in Streets
console.log(`Our ${s.length} streets have a total length of ${totalLength} km, with an average of
${avgLength} km.`);
// Classify sizes
s.forEach(el => el.classifyStreet()); Loops through all of them and calls the classifyStreet method
}
reportParks(allParks);
reportStreets(allStreets);