Search
 
SCRIPT & CODE EXAMPLE
 

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
Comment

PREVIOUS NEXT
Code Example
Javascript :: set time slots with date in javascript 
Javascript :: Repeat a String Repeat a String-Javascript 
Javascript :: js check if object key exists 
Javascript :: javascript sanitize html 
Javascript :: vue file 
Javascript :: what is shortest javascript program 
Javascript :: javascript substr 
Javascript :: recursive function javascript 
Javascript :: console 
Javascript :: how to download an mp3 file in react native 
Javascript :: bind json data to dropdownlist using javascript 
Javascript :: js promisify function 
Javascript :: js get formatted time 
Javascript :: script src in folder 
Javascript :: javascript variables 
Javascript :: javascript select letter in string 
Javascript :: node js code for saving first middle and last name 
Javascript :: data-toggle="tooltip not working due to jquery-ui.min.js 
Javascript :: atoi javascript 
Javascript :: JS cast a Number into a String 
Javascript :: react component 
Javascript :: delay external javascript file load 
Javascript :: convert json to 2d array 
Javascript :: js example 
Javascript :: put image in canvas with cover mode 
Javascript :: js alert with multiple buttons 
Javascript :: Angular Mat-Table with Dynamic Columns generate and Data should be populated in horizontal way 
Javascript :: textbox value length in javascript 
Javascript :: json traversal in js 
Javascript :: inline null check javascript 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =