7.3.13--Write a function called average, it takes an array parameter and returns the average of the items. Check if all the array items are number types. If not give return reasonable feedback. #73
7.3.13--Write a function called average, it takes an array parameter and returns the average of the items. Check if all the array items are number types. If not give return reasonable feedback.
function average(arr)
{
var sum=0;
var notN=0;
for(let i=0;i<arr.length;i++)
{
if(typeof(arr[i])!=typeof(1)){
notN=1;
}
var p=parseInt(arr[i]);
sum+=p;
}
var avg=(sum/arr.length);
if(notN==1)console.log(`type of all array item is not number type`)
return parseInt(avg);
}
console.log(`average is:`,average([1,2,2,9]));`
7.3.13--Write a function called average, it takes an array parameter and returns the average of the items. Check if all the array items are number types. If not give return reasonable feedback.
output
input
console.log(
average is:
,average([1,2,2,"7",9]));output