Advertisement
Spocoman

01. Sum Digits

Sep 19th, 2022
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function sumDigits(num) {
  2.     let strArray = num.toString().split('');
  3.     let sum = 0;
  4.  
  5.     for (let i = 0; i < strArray.length; i++) {
  6.         sum += Number(strArray[i]);
  7.     }
  8.  
  9.     console.log(sum);
  10. }
  11.  
  12. Тарикатско решение:)
  13.  
  14. function sumDigits(num) {
  15.     console.log(num.toString().split('').reduce((a, b) => Number(a) + Number(b), 0));
  16. }
  17.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement