You are constructing your arrays the wrong way:
gpa: [2.3, 3.7, 4.0] // no need for string concatenation here
Sum up all the values using a simple
for
loop- Divide the sum by the
length
of the array
Finding the average from an array [closed]
-
19-06-2023 - |
题
I'm here learning javascript. And I'm trying to make a function that will take in all the gpa of each student, find the average and console log it.
var stuObj = [{
name: " Alexandra Williams",
address: {
address: " 297 S Wilton Drive, Somewhere AR"
},
gpa: [2.5 + ', ' + 3.5 + ', ' + 4.0]
},
{
name: "Kallie Johnson",
address: {
address: " 84B NNE 332 Street', Queens NY"
},
gpa: [2.3 + ', ' + 3.7 + ', ' + 4.0]
}
];
解决方案
不隶属于 StackOverflow