Sum the properties
importance: 5
There is a salaries
object with arbitrary number of salaries.
Write the function sumSalaries(salaries)
that returns the sum of all salaries using Object.values
and the for..of
loop.
If salaries
is empty, then the result must be 0
.
For instance:
let salaries = {
"John": 100,
"Pete": 300,
"Mary": 250
};
alert( sumSalaries(salaries) ); // 650
function sumSalaries(salaries) {
let sum = 0;
for (let salary of Object.values(salaries)) {
sum += salary;
}
return sum; // 650
}
let salaries = {
"John": 100,
"Pete": 300,
"Mary": 250
};
alert( sumSalaries(salaries) ); // 650
Or, optionally, we could also get the sum using Object.values
and reduce
:
// reduce loops over array of salaries,
// adding them up
// and returns the result
function sumSalaries(salaries) {
return Object.values(salaries).reduce((a, b) => a + b, 0) // 650
}