Question: function getAverage ( nums ) { const sum = nums.reduce ( ( acc , val ) = > acc + val, 0 ) ; return

function getAverage(nums){
const sum = nums.reduce((acc, val)=> acc + val, 0);
return sum / nums.length;
}
function highestAverage(numsList){
let highestAverage =-Infinity;
let highestIndex =-1;
for (let i =0; i < numsList.length; i++){
const subArraySum = numsList[i].reduce((sum, num)=> sum + num, 0);
const subArrayAverage = subArraySum / numsList[i].length;
if (subArrayAverage > highestAverage){
highestAverage = subArrayAverage;
highestIndex = i;
}
}
return highestIndex;
}
// TESTS
// DO NOT MODIFY ANYTHING BELOW THIS LINE
const arrayA =[1,2,3,4,5];
const arrayB =[10000,-9998];
const arrayC =[2,100,55,19];
const arrayD =[4,8,12];
let score =0;
if (getAverage(arrayA)===3) score++;
if (getAverage(arrayB)===1) score++;
if (getAverage(arrayC)===44) score++;
if (getAverage(arrayD)===8) score++;
//if (highestAverage([arrayA, arrayB, arrayC, arrayD])===2) score++;
//if (highestAverage([arrayA, arrayB])===0) score++;
//if (highestAverage([arrayA, arrayD])===1) score++;
console.log("You have scored "+ score +"/7 points.");

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Finance Questions!