I am Learning Javascript, after doing Java development for 2 years. I am doing a module on udemy which had a coding challenge which I completed and it works but wanted to get it reviewed to find out best practices and how the code can be improved and optimised.
Here is the Challenge:
CODING CHALLENGE
Suppose that you're working in a small town administration, and you're in charge of two town elements:
- Parks
- 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:
- Tree density of each park in the town (forumla: number of trees/park area) √
- Average age of all of the town's parks (forumla: sum of all ages/number of parks) √
- The name of the park that has more than 1000 trees √
- Total and average length of the town's streets √
- 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.
My code is:
class NameAndBuildYear {
constructor(name, buildYear) {
this.name = name;
this.buildYear = buildYear;
}
}
class Park extends NameAndBuildYear {
constructor(name, buildYear, numOfTrees, parkArea) {
super(name, buildYear);
this.numOfTrees = numOfTrees;
this.parkArea = parkArea;
}
calculateTreeDensity() {
return this.numOfTrees / this.parkArea
}
calculateAgeOfTree() {
let age = new Date().getFullYear() - this.buildYear;
return age;
}
}
class Street extends NameAndBuildYear {
constructor(name, buildYear, streetLength, size = 'normal') {
super(name, buildYear);
this.streetLength = streetLength;
this.size = size;
}
}
let mordernPark = new Park('Mordern Park', 1909, 100, 0.2);
let tootingPark = new Park('Tooting Park', 1950, 1000, 0.4);
let balhamPark = new Park('Balham Park', 1800, 600, 2.9);
let parkArray = [mordernPark, tootingPark, balhamPark];
function calculateAverageAgeOfPark() {
var sumAge = 0;
parkArray.forEach((element) => {
sumAge += element.calculateAgeOfTree();
});
console.log(`The Average age of the the towns park is ${Math.round(sumAge / parkArray.length)} years`);
}
function calculateTreeDensityOfEachPark() {
for (const cur of parkArray) {
console.log(`The Tree Desnity of ${cur.name} is the density is ${Math.round(cur.calculateTreeDensity())} per square km`);
}
}
calculateTreeDensityOfEachPark();
calculateAverageAgeOfPark();
let numberOfTreesMap = new Map();
numberOfTreesMap.set(mordernPark.name, mordernPark.numOfTrees);
numberOfTreesMap.set(tootingPark.name, tootingPark.numOfTrees);
numberOfTreesMap.set(balhamPark.name, balhamPark.numOfTrees);
for (let [key, value] of numberOfTreesMap.entries()) {
if (value >= 1000) {
console.log(`${key}: has a ${value} trees`);
}
}
const faylandAvenue = new Street('Fayland Avenue', 1982, 200);
const tootingBroadWay = new Street('Tooting Broadway', 1908, 1000, 'huge');
const penworthamRoad = new Street('Penwortham Road', 1950, 300, 'big');
const mertonRoad = new Street('Merton Road', 1932, 100, 'small');
let streetMap = new Map();
streetMap.set(faylandAvenue.name, faylandAvenue.streetLength);
streetMap.set(tootingBroadWay.name, tootingBroadWay.streetLength);
streetMap.set(penworthamRoad.name, penworthamRoad.streetLength);
streetMap.set(mertonRoad.name, mertonRoad.streetLength);
function calculateTotalAndAverageStreetLength() {
let totalStreetLength = 0;
for (let [key, value] of numberOfTreesMap.entries()) {
totalStreetLength += value;
}
console.log(`The total length of the town's streets is ${totalStreetLength} meters and the average length is ${totalStreetLength / streetMap.size} meters`);
}
calculateTotalAndAverageStreetLength();
let streetArray = [faylandAvenue, tootingBroadWay, penworthamRoad, mertonRoad];
streetArray.forEach((element) => {
console.log(`The size of ${element.name} is ${element.size}`);
});