You are given a JavaScript function arrayAveragear that pass
You are given a JavaScript function arrayAverage(ar) that, passed a one-dimensional
array ar of numbers, returns the average of the number is ar. For example,
arrayAveraqe ( [1, 2, 3, 4 ] )
Returns 2.5.
Write a JavaScript function averageProps(obj) that takes an object obj whose property
values are one-dimensional arrays of numbers. It returns an object with the same properties (that
is, keys) as obj but the corresponding values are the averages of the numbers in the associated
Arrays. For example,
averageProps( { Al: [3,5,7], Ed: [2,4], Ken: [4] } )
Returns the object
(Al: 5, Ed: 3, Ken: 4}
Solution
function arrayAverage(ar)
{
var total=0;
for (var i=0;i<ar.length;i++)
{
total=total+ar[i];
}
var avg=total/ar.length;
return avg;
}
function averageProps(obj)
{
var objA={};
for (var key in obj)
{
objA[key]=arrayAverage(obj[key]);
}
return objA;
}
