Number



  • let num = prompt('Введите число')
    

    for (let i = 0; i < num.length; i++) {
    x = Number(num[i]) + Number(num[i + 1])
    alert(Сумма всех цифр числа: ${x})
    }

    Here's my guess, but in my case, when all the numbers are the same, the first two digits are summarised.



  • function fSumAllDigits(n) {
      return [...n.toString()].reduce((acc, el) => acc + parseInt(el), 0);
    }
    console.log( fSumAllDigits(111) ); // 3
    console.log( fSumAllDigits('123') ); // 6
    console.log( fSumAllDigits(700000001) ); // 8


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2