-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathES2015_FeaturePractice.js
69 lines (58 loc) · 2.52 KB
/
ES2015_FeaturePractice.js
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
class TownInfrastructure {
constructor (name, buildYear) {
this.name = name;
this.buildYear = buildYear;
}
}
class Park extends TownInfrastructure {
constructor (name, buildYear, numberOfTrees, parkArea) {
super(name, buildYear);
this.numberOfTrees = numberOfTrees;
this.parkArea = parkArea;
}
treeDensity() {
let density = Math.round(this.numberOfTrees/this.parkArea);
console.log(`The tree densitiy of ${this.name} is ${density} trees per m²`);
}
}
class Street extends TownInfrastructure {
constructor (name, buildYear, streetLength, size = 3) {
super(name, buildYear);
this.streetLength = streetLength;
this.size = size;
}
streetClassification() {
const classifications = new Map();
classifications.set(1, 'tiny');
classifications.set(2, 'small');
classifications.set(3, 'normal');
classifications.set(4, 'big');
classifications.set(5, 'huge');
console.log(`The street classication of ${this.name} is ${classifications.get(this.size)}`);
}
}
const parks = [new Park ("Neighbourhood Park", 1978, 195, 0.5), new Park ("City Park", 1988, 520, 2.3), new Park ("Adventure Park", 2014, 1750, 8.7)];
const streets = [new Street ("Nuremburg Street", 1968, 47), new Street ("Hanauer Street", 1973, 68, 4), new Street ("Garchinger Gasse", 1993, 20, 2),
new Street ("Leopold Street", 2002, 120, 5)];
function calcAverage(arr) {
const sum = arr.reduce((prev, curr) => prev + curr, 0);
return [sum, sum/arr.length];
}
function reportParks (p) {
console.log("----- PARKS REPORT -----");
const currentYear = new Date().getFullYear();
const ages = p.map(element => currentYear - element.buildYear);
const [totalAge, avgAge] = calcAverage(ages);
const indexOver1000Trees = p.map(element => element.numberOfTrees).findIndex(element => element >= 1000);
p.forEach(element => element.treeDensity());
console.log(`${p.length} parks have an average age of ${Math.round(avgAge)} years`)
console.log(`The ${p[indexOver1000Trees].name} has over 1000 trees`)
}
function reportStreets (s) {
console.log("----- STREETS REPORT -----");
const [totalLength, avgLength] = calcAverage(s.map(element => element.streetLength));
console.log(`Our ${s.length} streets have an average length of ${Math.round(avgLength)} m and a total length of ${totalLength} m`)
s.forEach(element => element.streetClassification());
}
reportParks(parks);
reportStreets(streets);