1

goal: take a number like 54321, add the numbers together (5+4+3+2+1 = 15), then take that number (15) add the digits (1+5 = 6), so return 6;

here is my code:

function digital_root(n) {

 if (n >=10) {
  var digits = n.toString().split('').map(function(item, index) {return parseInt(item)}).reduce(function(a,b){ return a+b});
   console.log(digits);
 } 
}

digital_root(1632)

Can't figure out: How to get that function to repeat over and over until digits is just one number (i.e. less than 10). I have tried a variety of nested functions, but can't seem to get it right.

If possible please point me in the direction to the solution ("try a nesting in a while... or read up on..."), but don't give me the complete code solution ("Use this code chunk:...."). I've developed a bad habit of just reading and copying...

Thank you!

2
  • My first suggestion for a non-expert would be to get rid of the map. Map is often conceptually challenging even for someone with lots of experience. Instead try an explicit loop, where when finding digits you need to use '/10' and '%10'. Good luck.
    – Stephen Rauch
    Jan 17, 2017 at 4:26
  • Thank you! I'll try and nix the map and work it out in a loop as you suggest!
    – Megan
    Jan 17, 2017 at 5:12

4 Answers 4

2

Try this: reference HERE

function digital_root(n) {

    var singlesum = 0;
    while (n >= 10 ) {
                 singlesum=0;
        while (n > 0) {
            var rem;
            rem = n % 10;
            singlesum = singlesum + rem;
            n = parseInt(n / 10);
        }
        n = singlesum;
    }
  
  console.log(singlesum);
}

digital_root(1632)

1
  • Thank you the link's chart definitely put visually what I was trying to map in my head.
    – Megan
    Jan 19, 2017 at 0:41
1

You can use recursion to solve this. Write a function makeSingleDigit, which argument will be your number. You need a base condition with the base step, which in your case stops the recursion when received number is one-digit and returns the number. If condition is not true, you just need to get another digit from the number by n%10 and sum it with the makeSingleDigit(Math.floor(n/10)). By this, you repeatedly sum digits of new numbers, until function receives one-digit number.

Mathematical solution just for your information: the number, which you want to find is n % 9 === 0 ? 9 : n % 9, thus it is the remainder of the division by 9 if it is not 0, otherwise it is 9.

0
1

Here is a very optimal solution to the problem:

function digital_root(n) {
  return (n - 1) % 9 + 1;
}

const result = digital_root(1632);
console.log(result);

1
  • I'm still trying to figure out how this works -- but it is a FANTASTIC solution, and should not be overlooked
    – Nemesarial
    Feb 19, 2021 at 13:28
0

Well, not a very good solution but you can give a hit.

function digital_root(n) {

 if (n >=10) {
  var digits = n.toString().split('').map(function(item, index) {return parseInt(item)}).reduce(function(a,b){ return a+b});
   console.log(digits);
   return(digits);
 } 
}
var num = 1632;
do{
num = digital_root(num);
}while(num>10);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.