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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started