I wrote this javascript program to find sum of array elements, also i got the answer, but i want the answer to be in array format , how i can do that ?
let arr=[1,2,3,4,5]; let sum=0; for(let i=0; i<=arr.length-1; i++){ sum=sum+arr[i]; console.log(sum); }
here is the output i am getting
**output ==>** 1 3 6 10 15
this is the output i want
**expected output ==>** [1,3,6,10,15]
Advertisement
Answer
let arr=[1,2,3,4,5]; let sum=0; var newarr = [] for(let i=0; i<=arr.length-1; i++){ sum=sum+arr[i]; newarr.push(sum) } console.log(newarr);