Question

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]
    }
];
Était-ce utile?

La solution

  1. You are constructing your arrays the wrong way:

    gpa: [2.3, 3.7, 4.0] // no need for string concatenation here
    
  2. Sum up all the values using a simple for loop

  3. Divide the sum by the length of the array
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top