0

****Hi there everyone. I am new to java Script. Just finished functions. I was trying to calculate the sum of an array values. So in this case the desired output would have been 21 but whenever i log on console i keep getting the value 1 . Can someone help****

{
let array = [1,2,3,4,5,6] ;
let sum  = 0 ;


function array_sum (){

for (let i = 0 ; i < array.length ; i++){

sum = sum + array[i] ;
return sum;
}

}
console.log(array_sum());
Muhammad Shaeel
  • 117
  • 2
  • 8

1 Answers1

0
  1. You don't need to return anything from inside the for loop.

  2. The final sum would be stored in variable "sum". Try console.log(sum)

Karan Raina
  • 136
  • 1