Question: JavaScript var a = [10,8,12,17,18]; var total = 0; for(var i = 0; i < a.length; i++) { total += a[i]; } var avg =

JavaScript

var a = [10,8,12,17,18];

var total = 0;

for(var i = 0; i < a.length; i++) { total += a[i];

}

var avg = total / a.length;

console.log("total=" + total + " average="+ avg);

Refactor the above problem and define a function named stats which takes in an array as the 1st and only parameter and returns a string with the same result as the above problem. Run it this way: console.log( stats( [2,4,6,8,10] ) );

DO NOT LOG the output in the function, return the result from the function, the console will print it if it is the list thing or use console.log( on the function )

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 Databases Questions!