define a function that takes a list of numbers as an argument and returns the sum of the numbers in the list. in javascript

index.tsx
function sumList(list){
  let sum = 0;
  for(let i=0; i<list.length; i++){
    sum += list[i];
  }
  return sum;
}

//example usage
let numbers = [1, 2, 3, 4, 5];
console.log(sumList(numbers)); //Output: 15
207 chars
12 lines

In this code, we define a function sumList that takes a list of numbers as an argument and returns the sum of the numbers in the list. We initialize a variable sum to 0 and use a for loop to iterate over the list and add the numbers together. Finally, we return the sum.

gistlibby LogSnag