Be insensitive, and keep a lifelong growth mindset.

0%

ES6 Code Challenge and Solution

1. Problem Statement

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 (formula: number of trees/park area);
  2. Average age of each town’s park (formula: 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.

2. Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class Element {
constructor (name, buildYear) {
this.name = name;
this.buildYear = buildYear;
}
}

class Park extends Element {
constructor(name, buildYear, area, numTrees) {
super(name, buildYear);
this.area = area; //km2
this.numTrees = numTrees;
}

treeDensity() {
const density = this.numTrees / this.area;
console.log('${this.name} has a tree density of ${density} trees per square km.');
}
}

class Street extends Element {
constructor(name, buildYear, length, size) {
super(name, buildYear);
this.length = length;
this.size = size;
}

classifyStreet() {
const classification = new Map();
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.');
}
}

const allParks = [new Park('Green Park', 1987, 0.2, 215), 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('Evergree Street', 2008, 2.7, 2), new Street('4th Street', 2015, 0.8), new Street('Sunset Boulevard', 1982, 2.5, 5)];

function calc(arr) {
const sum = arr.reduce((prev, cur, index) => prev + cur, 0);
return [sum, sum / arr.length];
}


function reportParks(p) {
console.log(----PARKS REPORTS----);
// Density
p.forEach (el -> el.treeDensity());

// Average age
const ages = p.map(el => new Date().getFullYear() - el.buildYear);
const [totalAge, avgAge] = calc(ages);
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);
console.log('${p[i].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));
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());
}

reportParks(allParks);
reportStreets(allStreets);