Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. // CODING CHALLENGE
  2.  
  3. /*
  4.  
  5. Suppose that you're working in a small town administration, and you're in charge of two town elements:
  6. 1. Parks
  7. 2. Streets
  8.  
  9. 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.
  10.  
  11. At an end-of-year meeting, your boss wants a final report with the following:
  12. 1. Tree density of each park in the town (forumla: number of trees/park area)
  13. 2. Average age of each town's park (forumla: sum of all ages/number of parks)
  14. 3. The name of the park that has more than 1000 trees
  15. 4. Total and average length of the town's streets
  16. 5. Size classification of all streets: tiny/small/normal/big/huge. If the size is unknown, the default is normal
  17.  
  18. All the report data should be printed to the console.
  19.  
  20. HINT: Use some of the ES6 features: classes, subclasses, template strings, default parameters, maps, arrow functions, destructuring, etc.
  21.  
  22. */
  23.  
  24. class Park {
  25. constructor(name, builtYear, area, trees) {
  26. this.name = name;
  27. this.builtYear = builtYear;
  28. this.area = area;
  29. this.trees = trees;
  30. }
  31.  
  32. calculateAreaPark() {
  33. const calcArea = this.trees / this.area;
  34. console.log(`${this.name} has tree density of ${Math.floor(calcArea)}`);
  35. }
  36.  
  37. age() {
  38. const ages = new Date().getFullYear() - this.builtYear;
  39. return ages;
  40. }
  41.  
  42. moreThan1000() {
  43. if (this.trees >= 1000) {
  44. console.log(`${this.name} has more than a 1000 trees`);
  45. }
  46. }
  47. }
  48.  
  49. class Street {
  50. constructor(name, builtYear, length, size) {
  51. this.name = name;
  52. this.builtYear = builtYear;
  53. this.length = length;
  54. this.size = size;
  55. }
  56.  
  57. length() {
  58. this.length;
  59. }
  60.  
  61. size() {
  62. this.size;
  63. }
  64. }
  65.  
  66. const allParks = [
  67. new Park("Sedmikraskovy", 1995, 0.5, 135),
  68. new Park("Park SND", 1989, 0.7, 1700),
  69. new Park("Park Milady Horakovej", 1965, 1.3, 250)
  70. ];
  71.  
  72. const allStreets = [
  73. new Street("Visnova", 1990, 3.2, 12),
  74. new Street("Ceresnova", 1942, 1.5, 52),
  75. new Street("Masarykova", 1918, 2.6, 35),
  76. new Street("Gagarinova", 1950, 4.6, 76)
  77. ];
  78.  
  79. //1.
  80. allParks.forEach(current => {
  81. current.calculateAreaPark();
  82. });
  83.  
  84. //2.
  85. let allAges = () => {
  86. let result = 0;
  87. allParks.forEach(current => {
  88. let ages = current.age();
  89. result += ages;
  90. });
  91. return result;
  92. };
  93. console.log(
  94. `Our ${allParks.length} parks have an average age of ${allAges() /
  95. allParks.length} years.`
  96. );
  97.  
  98. //3.
  99. allParks.forEach(current => {
  100. current.moreThan1000();
  101. });
  102.  
  103. //4.total
  104. let totalLengthStreet = () => {
  105. let result = 0;
  106. allStreets.forEach(current => {
  107. let tot = current.length;
  108. result += tot;
  109. });
  110. return result;
  111. };
  112.  
  113. console.log(totalLengthStreet());
  114.  
  115. //4.average
  116. // total / pocet ulic
  117. console.log(`Average is ${totalLengthStreet() / allStreets.length}.`);
  118.  
  119. //5.
  120. // Calculate size
  121.  
  122. function calcSize(street) {
  123. if (street.size <= 20) {
  124. return "Small";
  125. } else if (street.size <= 30) {
  126. return "Normal";
  127. } else if (street.size <= 60) {
  128. return "Big";
  129. } else {
  130. return "Normal";
  131. }
  132. };
  133.  
  134. allStreets.forEach(function (street) {
  135. console.log(calcSize(street))
  136. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement