Is there a method for folding numbers?

For example:

15 = 1 + 5 = 6 

or

 584 = 5 + 8 + 4 = 17 
  • one
    I wonder who set -1 ? The person asked the correct question, what is a simple question does not mean to ask? - Raz Galstyan
  • @RazmikGalstyan formally "The message contains only the text of the task, in which there is no description of the problem, or the question is purely formal." Perhaps someone did not like it. - Dmitry
  • 2
    in the second variant, where it turned out 17, it is not necessary to add up 1 and 7 again? - Sergiks

4 answers 4

You can take the balances and round off:

 function digsum(n) { var sum = 0; while(n) sum += n % 10, n = Math.floor(n / 10); return sum; } digsum(15) // 6 digsum(584) // 17 

     function sum(a) { return a.toString().split('').reduce(function(a, b) { return a + parseInt(b); }, 0); } console.log(sum(584)) 

    ES2015

     function sum(a) { return a.toString().split('').reduce((a, b) => a + parseInt(b), 0); } console.log(sum(584)) 

    • 2
      The answer is not bad, but it would not be bad to give an explanation of how the function mechanism is implemented. Well, for example, split - splits a string into an array of strings, reduce - applies a function to each value of the array, from left to right, reducing it to a single value. And so +1 :) - Denis Bubnov
    • @DenisBubnov agrees with you. I was in a hurry, I wanted to shine with my mind. In turn, plus your comment, as it adds useful information to the message. - Dmitry
    • one
      You can, by the way, shorten the code a bit by using +b instead of parseInt(b) . But this is more than "number of characters" vs "ease of understanding of the code." - Regent

    it seems like there is no ready method, you can write it yourself, break the string into characters and put them together

     function sum(number) { var digits, sum = 0; if(Object.prototype.toString.call(number) == '[object Array]') { // из комментов - либо (number instanceof Array) digits = number; } else { digits = number.toString().split('') } for(var i = 0; i < digits.length; i++) { sum += parseInt(digits[i]); } return sum; } 
    • and if we have an array? - user255352
    • in conditions it was not said;) now we will fix it - maxkrasnov
    • 2
      @ user255352 а если у нас массив? - you do not know what cycles are and how to calculate the sum of all numbers using it? maybe then you read a little book on bases of language? - Alexey Shimansky
    • Alexey I read))) thanks for answering - user255352
    • @maxkrasnov I somehow did not understand the logic of the updated version of your answer? It seems to me not working correctly. - Raz Galstyan

    There is no such method.

     function digits_sum(number){ let result = 0; for(let i = 1; i<number; i=i*10) result += ((number-number%i)/i)%10; return result; } console.log(digits_sum(1234)); console.log(digits_sum(88)); console.log(digits_sum(5555555555)); 

    • 2
      The code does not work correctly for numbers 1 , 10 , 100 , etc. Must be i <= number . As for i = i * 10 instead of i *= 10 and saving on spaces, this is already a subjective matter. - Regent