3 functions to compute totals

by thewolff

JavaScript

const arr = [10,14,6]
 
const totalFor = (list) => {
	let total = 0;
	for (var i = 0, len = list.length; i < len; i++) {
	total += list[i]
  console.log(total)
	}
return total;
}
 
console.log(totalFor(arr))

const totalWhile = (list) => {
	let total = 0;
	let counter = 0;
	const len = list.length
	while(counter < len) {
		total += list[counter]
		counter++
}
return total;
}
 
console.log(totalWhile(arr));

function sumRec(array, acc = 0, index) {
    //We will update our accumulator, and increment
  // the value of our current index
  return index === array.length
  ? acc
  : sumRec(array, acc += array[index], ++index);
}

console.log(sumRec(arr, 0, 0));