title: Javascript - How to sum up all the first and last digit of a number until only two digits are left?
body: <p>I am fairly new to programming, just knowing the basics in Javascript and Swift. I am trying to write a simple program which creates, from any entered number, a two digit number by summing up the first and last digit until only two digits are finally left. </p>
  
  <p>Here is an example:</p>
  
  <pre><code>num = 1234567:
  
  1+7 = 8
  2+6 = 8
  3+5 = 8
  4 remains
  
  So the first result is: 8884. Now everything is added again:
  
  8+8 = 16
  8+4 = 12
  
  The result is 1612. Again everything is summed up:
  
  1+2 = 3
  6+1 = 7
  
  The result is 37 - which is also the final result.
  </code></pre>
  
  <p>I am struggling with two things. First the while loop. I was thinking about casting num.toString() and then do a while loop like this in which I change the string to an int again:</p>
  
  <pre><code>num.toString()
  while (num.length &gt; 2) {
      num = num.parseInt(num, 10);
      ...
  }
  </code></pre>
  
  <p>But this doesn't work properly, plus it gets crazy complicated I guess because I would have to switch between string and int each new round, right?</p>
  
  <p>I know how to add all digits together until I get a two digit number (it took me a while to figure this one out) and I am not even sure if this is a good way to do it:</p>
  
  <pre><code>var sum = num
      .toString()
      .split('')
      .map(Number)
      .reduce(function (a, b) {
          return a + b;
      }, 0);
  </code></pre>
  
  <p>But obviously I cannot use this here and I have no idea how to change the code so that the first and last digit are added together.</p>