How can I find average mark

Sorry if I don’t write good, it’s my first post. I have a problem with one task.

I have a test for a class and I want to know the average results for the class. Write a function that takes an array of hash with all of the grades/marks for a given test and returns an average grade for the entire class. Input are in JSON

function my_average_mark(param_1) {    
let total = 0
let result = 0;
    for (const key of param_1) {
    if (param_1.length > 0){
        total += (key['integer'])
        result=(total/param_1.length)   
    }
}
return result.toFixed(1)
};

These checks are successful

1)

Input: [
        {"string": "John", "integer": 7},
        {"string": "Margot", "integer": 8},
        {"string": "Jules", "integer": 4},
        {"string": "Marco", "integer": 19}
       ]

Output: 9.5
  1. Input: []
    Output: 0.0

But I have trouble with this Input: {} – I get TypeError: param_1 is not iterable

65 thoughts on “How can I find average mark”

Leave a Comment