find the sum of a list of numbers in typescript

To find the sum of a list of numbers in TypeScript, you can use a loop to iterate over the array and add each number to a running total. Here's an example function:

index.ts
function sum(numbers: number[]): number {
  let total = 0;
  for (let i = 0; i < numbers.length; i++) {
    total += numbers[i];
  }
  return total;
}
151 chars
8 lines

This function takes an array of numbers as an argument and returns the sum of the numbers. The total variable starts at 0, and the loop adds each number in the array to total. Finally, the function returns total.

gistlibby LogSnag