user4609276
user4609276

Reputation:

Recursion not properly running?

I'm practicing recursion, and am trying to use it to constantly add individual digits in a number until there is only 1 digit left.

Basically, if the number is 84, it becomes 8+4 = 12 which then becomes 1 + 2 = 3.

Below is my attempt on it. Not sure what I'm missing..

const weirdSum = (num) => {
  let result = 0;    
  const split = num.toString().split('');

  if(split.length > 1){
    for(let i=0;i<split.length;i++){
      result = result + (split[i]*1);
    }           
    weirdSum(result); // pass result as argument, which will be split.
  }

  return result; // return result if split.length is 1
}

Upvotes: 1

Views: 80

Answers (5)

James
James

Reputation: 22237

const weirdSum = num => {
  // figure out the sum
  const sum = [...num + ""].reduce((a, e) => a + (e - 0), 0);
  // recurse if necessary
  return sum < 10 ? sum : weirdSum(sum);
}

console.log(weirdSum(84));

Upvotes: 0

Al Hill
Al Hill

Reputation: 479

let weirdSum = num => {  
  const split = num.toString().split('');

  if(split.length > 1){
    const sum = split.reduce((acc, it) => parseInt(it) + acc, 0)        
    return weirdSum(sum);
  }

  return num;
}

console.log(weirdSum(84));
console.log(weirdSum(123456));

Upvotes: 1

saonnet
saonnet

Reputation: 110

This function isn't anonymous but it solves your problem

function weirdSum(num) {

    if(parseInt(num/10) == 0) {
        return num;
    }

    var num1 = 0;

    while(num != 0) {
        var d = parseInt(num%10);
        num1=num1+d;            
        num=parseInt(num/10);
    }

    return weirdSum(num1);

}

How it works ?

  • Any single digit number when divided by 10 gives 0 as quotient ( parsing to int is required ), function exists when this condition is met ( the first if ).
  • In the while loop I'm extracting digits of the number ( starting from the last digit ), when we divide a number by 10, the remainder is always the same as the last digit, then we are adding this to the new num ( num1 ).
  • In the last step of the while loop, we are shortening the number by removing is last digit by dividing it by 10 and repacing the old num1 by quoatient.

Upvotes: 0

Sanjay Nishad
Sanjay Nishad

Reputation: 1605

there are 2 mistakes, one you need to return weirdSum(result); another you are returning result which is 0 you should return num

const weirdSum = (num) => {
  let result = 0;    
  const split = num.toString().split('');

  if(split.length > 1){
    for(let i=0;i<split.length;i++){
      result = result + (split[i]*1);
    }           
    return weirdSum(result); // pass result as argument, which will be     split.
  }

  return num; // return result if split.length is 1
}

console.log(weirdSum(84));

Upvotes: 1

Vahid Alimohamadi
Vahid Alimohamadi

Reputation: 5858

const weirdSum = (num) => {
  let result = 0;    
  const split = num.toString().split('');

  if(split.length > 1){
    for(let i=0;i<split.length;i++){
      result = result + (split[i]*1);
    }           
    return weirdSum(result); // pass result as argument, which will be split.
  }
  
  return split[0]; // return result if split.length is 1
}

console.log(weirdSum(84))

I just changed your codes to works properly, I have no idea it is optimized or not. You need ti return the split[0] when recursion stack ends not the result!

Upvotes: 0

Related Questions