Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
JavaScript
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;
  }
}


What I have tried:

Output:

{ S: 1, a: 1, m: 1, J: 1, o: 1, h: 1, n: 1 }
Posted
Updated 4-Oct-21 22:28pm
v2
Comments
Chris Copeland 5-Oct-21 6:05am    
This isn't a very clear request. Is the output that you've provided what you expect the produced content to be, or is it what is currently being produced? Also, what do the numbers in parenthesis mean?

You can already store the result of that function in one variable: var value = sum(1234);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900