0

so I am very new to coding and currently in a coding camp. We have just started Javascript functions and I am trying to practice. The below code is just saying sum not defined, when I remove the let in sum and just sum =0; then it only take into account index 0 and doesn't loop through the whole array, can someone let me know what I'm doing wrong.

let arr = [2, 3, 5];
let z = avgit();

function avgit() {
    let sum = 0;
    for (var i=0; i < arr.length; i++) {
        sum += arr[i];
        return sum/arr.length;
    }
}


console.log(sum);
console.log(z);

I want the code to find the total sum of the arr and then average it by dividing by arr.length.

Ify_dev
  • 1
  • 1

0 Answers0