Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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.
- */
- class Park {
- constructor(name, builtYear, area, trees) {
- this.name = name;
- this.builtYear = builtYear;
- this.area = area;
- this.trees = trees;
- }
- calculateAreaPark() {
- const calcArea = this.trees / this.area;
- console.log(`${this.name} has tree density of ${Math.floor(calcArea)}`);
- }
- age() {
- const ages = new Date().getFullYear() - this.builtYear;
- return ages;
- }
- moreThan1000() {
- if (this.trees >= 1000) {
- console.log(`${this.name} has more than a 1000 trees`);
- }
- }
- }
- class Street {
- constructor(name, builtYear, length, size) {
- this.name = name;
- this.builtYear = builtYear;
- this.length = length;
- this.size = size;
- }
- length() {
- this.length;
- }
- size() {
- this.size;
- }
- }
- const allParks = [
- new Park("Sedmikraskovy", 1995, 0.5, 135),
- new Park("Park SND", 1989, 0.7, 1700),
- new Park("Park Milady Horakovej", 1965, 1.3, 250)
- ];
- const allStreets = [
- new Street("Visnova", 1990, 3.2, 12),
- new Street("Ceresnova", 1942, 1.5, 52),
- new Street("Masarykova", 1918, 2.6, 35),
- new Street("Gagarinova", 1950, 4.6, 76)
- ];
- //1.
- allParks.forEach(current => {
- current.calculateAreaPark();
- });
- //2.
- let allAges = () => {
- let result = 0;
- allParks.forEach(current => {
- let ages = current.age();
- result += ages;
- });
- return result;
- };
- console.log(
- `Our ${allParks.length} parks have an average age of ${allAges() /
- allParks.length} years.`
- );
- //3.
- allParks.forEach(current => {
- current.moreThan1000();
- });
- //4.total
- let totalLengthStreet = () => {
- let result = 0;
- allStreets.forEach(current => {
- let tot = current.length;
- result += tot;
- });
- return result;
- };
- console.log(totalLengthStreet());
- //4.average
- // total / pocet ulic
- console.log(`Average is ${totalLengthStreet() / allStreets.length}.`);
- //5.
- // Calculate size
- function calcSize(street) {
- if (street.size <= 20) {
- return "Small";
- } else if (street.size <= 30) {
- return "Normal";
- } else if (street.size <= 60) {
- return "Big";
- } else {
- return "Normal";
- }
- };
- allStreets.forEach(function (street) {
- console.log(calcSize(street))
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement