Advertisement
Didart

Sum of Numbers

Apr 5th, 2022
1,006
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function sumOfNumbers(input) {
  2.     let digitAsText = "" + input[0];
  3.  
  4.     let sum = 0;
  5.     for (let index = 0; index < digitAsText.length; index++) {
  6.         let n = Number(digitAsText.charAt(index));
  7.         sum += n;  
  8.     }
  9.  
  10. console.log(`The sum of the digits is:${sum}`);
  11.  
  12. }
  13. sumOfNumbers(["1234"])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement