How to sum the numbers in an array with TypeScript

2 Answers

0 votes
const arr = [6, 8, 1, 2, 5, 9, 3, 4];
  
const sum = arr.reduce((x, y) => x + y);

console.log(sum); 
  
  
  
    
      
      
/*
run:
      
38 
      
*/

 



answered Jun 29, 2022 by avibootz
0 votes
const arr = [6, 8, 1, 2, 5, 9, 3, 4];
  
let sum = 0;
 
for (let num of arr) {
    sum = sum + num
}
  
console.log(sum); 
  
  
  
    
      
      
/*
run:
      
38 
      
*/

 



answered Jun 29, 2022 by avibootz
...