0

I am trying to add these integer values from this Array object to get the sum and in turn the average.

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

Output: 9.5

I have been able to do this function, however, I am not able to add the values from the integer into a sum variable and divide by the objectName.length. My sample code is beneath, I want to understand why it is not working?

function my_average_mark(student_grades) {
    sum = 0;
    for( i = 0; i < student_grades.length; i++){
        // console.log(student_grades[i].integer);
        sum += student_grades[i].integer;
        console.log(sum);
        
    }
    
};


all_grades = [
        {"string": "John", "integer": 7},
        {"string": "Margot", "integer": 8},
        {"string": "Jules", "integer": 4},
        {"string": "Marco", "integer": 19}
       ]
console.log(my_average_mark(all_grades));
4
  • There's no such thing as a "JSON Object" (or a "JSON array object") - Input and all_grades are arrays of objects... Commented Mar 16, 2021 at 11:16
  • Note you arent returning anything from your function, so the last log won't show anything Commented Mar 16, 2021 at 11:18
  • As @Nick already mentioned why it's not working the one line solution is something like this console.log(Input.reduce((n, {integer}) => n + integer, 0)/Input.length) Commented Mar 16, 2021 at 11:25
  • @PatrickEvans actually my last log printed out something. but it was adding up values for my integer values. Nick suggestion helped out. Thanks. Commented Mar 16, 2021 at 11:45

2 Answers 2

2
function my_average_mark(student_grades) {
    sum = 0;
    for( i = 0; i < student_grades.length; i++){
        sum += student_grades[i].integer;        
    }
    return sum / student_grades.length;
};

consider using array functions like map and reduce

function my_average_mark(student_grades) {
    sum = student_grades.map(p => p.integer).reduce((a,b) => a+b, 0)
    return sum / student_grades.length;
};
Sign up to request clarification or add additional context in comments.

Comments

1

Your code is almost right, you just need to return the sum divided by the length:

function my_average_mark(student_grades) {
  let sum = 0;
  let len = student_grades.length;
  for (let i = 0; i < len; i++) {
    sum += student_grades[i].integer;
  }
  return Math.round(sum * 10 / len) / 10;
};


all_grades = [
        {"string": "John", "integer": 7},
        {"string": "Margot", "integer": 8},
        {"string": "Jules", "integer": 4},
        {"string": "Marco", "integer": 19}
       ]
console.log(my_average_mark(all_grades));

Note you should always declare your variables using var, let or const.

1 Comment

@AsanteMichael I modified your edit to use Math.round to round to 1 decimal place instead; using toFixed would mean that the function would return a string instead of a number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.