We have an array with the name; chess_players, in each element of the array there is an object with two properties: the name of a chess player and the points he has obtained. In this activity, a function must be created (which allows code reuse, that is, if the table is extended with more players, the function must continue to work without having to modify anything). The created function must receive the object as a parameter and return the name of the chess player who has obtained the most points.
This is what I tried:
let chess_players = [{name:"Jackson",points:[900,1000,3000,1950,5000]},{name:"Steve",points:[300,400,900,1000,2020]}]
function returnName(object){
/* With this for in loop, I'm trying to iterate through each array to calculate the sum of each array */
for (var num in object){
var index = 0;
var length = object.length;
var sum = 0;
/* I try to return the maximum value */
sum += object.fact[index ++]
var maxVal = Math.max(...sum);
}
return array[index].name;
}
console.log(returnName(chess_players))