Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

sum up all the first and last digit of a number until only two digits are left

function sum(num) {
  var numString = num.toString();
  var newString = "";
  while (numString.length > 1) { // (1)
    newString += (parseInt(numString[0]) + parseInt(numString[numString.length - 1])).toString(); // (2)
    numString = numString.substring(1, numString.length - 1); // (3)
  }
  newString += numString; // (4)

  if (newString.length > 2) { // (5)
    console.log(newString)
    return sum(newString);
  } else {
    return newString;
  }
}


console.log(sum(1234567));
 Run code snippet
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #sum #digit #number #digits #left
ADD COMMENT
Topic
Name
8+8 =