JavaScript Code

How to get average of numbers in array?

Problem Statement

given an array of numbers, write a JavaScript program to find the average of these numbers in array


to find the average of numbers in array, first find the sum using a for loop and iterating over the elements of array, and then divide the sum with the length of the array


given array is nums. find the average

let nums = [4, 8, -3, 41, 2, 0, 6];
let sum = 0;
for (let index = 0; index < nums.length; index++ ) {
    sum += nums[index];
let average = sum / nums.length;