title: Understanding another Javascript snippet which add digits of a number
body: <p>While this answer at this question <a href="https://stackoverflow.com/questions/18626134/adding-digits-from-a-number-using-recursivity-javascript">here</a> was the best answer due to recursion aspect, </p>
  
  <pre><code>function sumDigits(number) {
      var remainder = number % 10;
      var sum = remainder;
      if(number &gt;= 10) {
          var rest = Math.floor(number / 10);
          sum += sumDigits(rest); 
      }
      return sum;
  }
  </code></pre>
  
  <p>there was another answer that seems easier to understand. However, there were some parts of the answer that I don't understand. </p>
  
  <pre><code>function sumDigits(number) {
    var sum = 0;
    var numbers = number.toString().split(""); //turn the number to string and split the number in several digits
  
    while(numbers.length &gt; 0) {
      sum += parseInt(numbers[0], 10);  // covert string to number and force the number to be decimal and add the next number 
      numbers.splice(0, 1);  //remove the first digit, in other words, removing "0" from sum, i think?
      sumDigits(numbers.join('')); //join the array element into a string
    }
  
    return sum;
  }
  
  var sum = sumDigits(175);
  $('#result').html(sum);
  </code></pre>
  
  <p>Here's what I was able to understand:</p>
  
  <pre><code>var numbers = number.toString().split("");
  </code></pre>
  
  <p>convert the number to string and split the string into an array of substrings. In other words, if the number is 175, it will be converted to "175" and split into 1, 7, 5. Next,</p>
  
  <pre><code>while(numbers.length &gt; 0) {
  </code></pre>
  
  <p>This ensure that the number is positive and can be added. However, when I removed this line and ran it in JS Bin with a negative number, it returns an error. So somehow, this is important to sum up only the positive numbers. Next,</p>
  
  <pre><code>sum += parseInt(numbers[0], 10);
  </code></pre>
  
  <p>This (parseInt) convert the string or in our case, the substrings, back into numbers and add each number to itself. However I don't understand the "0" that's in the brackets after the <strong>numbers</strong> unless it's referring to the first number in the array = [1, 7, 5]?? Still, I can see that when I remove [0] from this line, it returns an error. Additional, with the "sum +=", it should be adding it up like this 0 + 1 + 7 + 5. Thus the sum is 13. Next,</p>
  
  <pre><code>numbers.splice(0, 1);
  </code></pre>
  
  <p>This is the part which I don't understand. I know that .splice either add or remove items. For example, the first part of the (0,1), which is "0", find the position of the array, in other words, "0" find the first position before "1" in the array = [1, 7, 5]. The last part, which is "1", specify the amount of items to be removed. In other words, it's asking to remove "1" from the array, causing now the array to be [7, 5]. However, I think it's not needed at all because there's no need to remove any digit from the number. Also, I don't understand the comment that said that this line remove "0" from the sum when the sum should be 13? Next,</p>
  
  <pre><code>sumDigits(numbers.join(''));
  </code></pre>
  
  <p>This is supposed to join the array elements into a string. In other words, it's supposed to join array = [1, 7, 5] into "1, 7, 5". Still, I don't understand why those two lines (.splice and .join) is needed when this line (sum += parseInt(numbers[0])) converts the string back into numbers and add the digits together. However when I did remove those two lines, the function doesn't work...</p>
  
  <p>Please help me to understand how it works and which is execute first and so on...</p>