Finding average in 2d array Javascript

Hello folks, I need some help

[[“Manutd”,4],[“Liverpool”,8],[“Chelsea”",4],[“Manutd”,8],[“Liverpool”,1]]

This string encodes a list of football scores of a football player against various matches. Each element of the list is a itself a list of two elements - the first is the name of the football team against which the match was held, and the second is the score of the goal.

Write a function/method in JavaScript to take such a string and return the football team which this football player has the highest average. Average score against a football team is defined as the sum of all the football goal this player scored in matches against that team, divided by the number of matches played against that team.

For example, if the above string is passed to your code, it should return “Manutd”. Remember, the input will be passed to you as a string.

//i have written half code, but stucked to go forward
function average(nums) {

const store=;

for(var i=0;i<nums.length;i++){

for(var j=0;j<nums[i].length;j++){

  if(nums[i][j]=="Manutd"){

    const sum;

    sum+=num[j][1];

  }

  console.log(nums[i][j]);

}

}

}

@html_guy Before you start writing any code, you need to first make sure you understand all the steps you would need to take to accomplish this task without a computer program.

First try to explain the steps you would take on the example shown above that would allow you to return “Manutd”. What steps and calculations would you need to make if I had given you a list of the teams and scores on paper like the following?

Manutd - 4
Liverpool - 8
Chelsea - 4
Manutd - 8
Liverpool - 1

Once you can write down the steps, then you can begin to convert those steps to actual code. Without a complete understanding of the steps to arrive at the solution for a given array (list) of teams/goals, you will struggle to achieve your goal.

Once you post your steps (your algorithm), we can make sure the algorithm will work before moving on the writing the code.

1 Like