germancin
6/1/2018 - 4:26 AM

simple-SUM-Single-For-O(n).js

function simpleArraySum(ar) {

    let sum = 0;
    for(let i=1; i< ar.length; i++){ //O(n)
      
      if(i === 1){
          sum = ar[i] + ar[i-1];
      }else{
          sum = ar[i] + sum;
      }

    }
    console.log(sum)
}

// console.log([ 1, 2, 3, 4, 10, 11 ]);
console.log(simpleArraySum([ 1, 2, 3, 4, 10, 11 ]))