var people =[{
name: "John",
number: "283.37"
},{
name: "Susan",
number: "125.44"
},{
name: "Karen",
number: "98.7"
}];
var sum = 0;
for (let i = 0; i < people.length; i++) {
sum += parseFloat(people[i].number);
}
console.log("average: ", sum / people.length);
You have some problems with the formatting here. Numbers should have a period, not a comma. Also, you need colons and not equals, with commas separating different properties.
var people =[{
name: "John",
number: "283.37"
},{
name: "Susan",
number: "125.44"
},{
name: "Karen",
number: "98.7"
}];
The next part is okay.
var sum = 0;
I won't go fully into the reasons, and you don't have to use it, but I prefer a regular for loop here, as so. You need to parseFloat your strings.
for (let i = 0; i < people.length; i++) {
// You have to make sure you parseFloat since the 'numbers' from the objects are in fact strings.
sum += parseFloat(people[i].number);
}
// Then you can return the average.
console.log(sum / people.length);
people.forEach(function(person) { sum += +person.number; });(you're iterating over persons, not numbers. You also need to do+person.numberto convert the string into an actual number first)