Assignment:
Write array that contains 5 objects. Every object represents person
with
Write a function
Write another function
Both functions needs to have one parameter for array to be passed to them.
name and year. Write a function
"addYears" which will go through given
array and add one year to every person. Write another function
yearAverage which will go through
the given array and calculate the average of years of all persons.
Both functions needs to have one parameter for array to be passed to them.
Solution:
var persons = [
{'name': 'John', "years": 25},
{'name': 'Joe', "years": 20},
{'name': 'Bertha', "years": 30},
{'name': 'Sabahudin', "years": 15},
{'name': 'Tomislav', "years": 35}
];
function addYears(persons) {
for (let i = 0; i < persons.length; i++) {
persons[i].years += 1;
}
return persons;
}
function yearAverage(persons) {
var totalSum = 0;
for (let i = 0; i < persons.length; i++) {
totalSum += persons[i].years;
}
return totalSum / persons.length;
}