• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Aug 17th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. const People = [
2.   {
3.     name: "Rene",
4.     Age: 33,
5.     weight: 110
6.   },
7.   {
8.     name: "Claire",
9.     Age: 27,
10.     weight: 65
11.   },
12.   {
13.     name: "Tom",
14.     Age: 19,
15.     weight: 75
16.   },
17.   {
18.     name: "Henry",
19.     Age: 40,
20.     weight: 120
21.   },
22.   {
23.     name: "Kate",
24.     Age: 44,
25.     weight: 50
26.   }
27. ];
28. // Write a function to determine the average age and weight of the group
29.
30. // Solution 1 (RenĂ©, Specific)
31. const getAverageSpecific = () => {
32.     let count = 0;
33.     let amount = People.length;
34.     People.map(person => {
35.       count += person.weight;
36.     });
37.     return  count / amount
38.   };
39. console.log(getAverageSpecific())
40.
41. // Solution 1 (RenĂ©, Reusable)
42. const getAverage = (array, key) => {
43.   let count = 0;
44.   let amount = array.length;
45.   array.map(res => {
46.     count += res[key];
47.   });
48.   return  count / amount
49. };
50.
51. console.log(getAverage(People, "weight"));
52. console.log(getAverage(People, "Age"));
53.
54. //Solution 2 (Nic)
55. function ageWeight() {
56.     let age = 0;
57.     let weight = 0;
58.     for (let i = 0; i < People.length; i++) {
59.         weight += People[i].weight;
60.         age += People[i].Age;
61.     }
62.     console.log(("Average age is: ")+(age / People.length));
63.     console.log(("Average weight is: ")+(weight / People.length));}
64. ageWeight()
65.
66. //Solution 3 (Karol)
67. function average(array){
68.     return {
69.         averageAge : array.map( item => item.Age ).reduce( (p,c) => p+c ) / array.length,
70.         averageWeight : array.map( item => item.weight ).reduce( (p,c) => p+c ) / array.length
71.     };
72. }
73.
74. let result = average(People);
75. console.log(result.averageAge);
76. console.log(result.averageWeight);
77.
78. //Solution 4 (Mojgan)
79. let sum = 0
80. for ( var i = 0; i < People.length; i ++){
81.     sum += People[i].Age
82. }
83. console.log(sum / People.length)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top