0

I'm writing a function which takes in a positive number and returns the number of times you must multiply the digits in num until you reach a single digit.

example:

test(39) === 3 // because 3*9 = 27, 2*7 = 14, 1*4=4
                   // and 4 has only one digit//if single digit return num

The code:

function test(num) {
if (num > 10) { return num} else {
   var j = num;var a;var count = 0;
while ( j > 10){
   a = num.toString().split('').map( e=> parseInt(e)).reduce((a,c)=> a*c);
   num = a;
   count++;
   j--; 
   }  
return count;
}}

test(39) //outputs 29 expected 3;

I have fixed the above by adding an array and filtering for unique values but would still like to know why the code is giving me a much higher count than expected.

3
  • 1
    If num is already below 10, shouldn't it return 0 and not the number itself? I mean you describe your function as returning the number of iterations, not a result from a calculation. Commented Nov 20, 2018 at 10:21
  • 2
    The j-- part is the bit that's making the result far too high. You can add console.log(a,num,j) inside the while to see what it's doing (or step through with the debugger). Also return num at the start makes no sense, should be return 0 as num<10 means it's already single digit so doesn't need to run your algorithm. Commented Nov 20, 2018 at 10:24
  • You're right,dunno why i even put that j part and forgot about it.Works without it. Commented Nov 20, 2018 at 11:44

4 Answers 4

2

You might be looking to use recursion here - if the num is smaller than 10, return 0, otherwise, perform the needed multiplication, and then return 1 + the result of calling test on the product:

function test(num) {
  return num < 10
  ? 0
  : 1 + test(
    num.toString().split('').reduce((a, c) => a * c)
  )
}

console.log(test(39));

(note that * will coerce strings to numbers already, no need for parseInt)

Sign up to request clarification or add additional context in comments.

5 Comments

What happened to if (num < 10) { return num }?
return num doesn't make sense if OP is trying to figure out the number of iterations required to produce a single digit number
@PeterB that's handled with the ?: = if (num<10) return 0;
True, description by OP does not seem to match what they tried.
Thanks guys.This works.Also the > sign in the first line was a type,now corrected
1

Correction in your code, you can compare

    function test(num) {
if (num < 10) { return num} else {
   var a;var count = 0;
while ( num > 10){
   a = num.toString().split('').map( e=> parseInt(e)).reduce((a,c)=> a*c);
   num = a;
   count++;

   }  
return count;
}}

test(39) //outputs 3;

Comments

0

You're decrementing j, which is initially 39. It will reach 10, then it will return the number of times it was decremented (29 times, which coincides with your result). You need to do the following, assuming the split part is correct:

function test(num) {
if (num < 10) { return num} else {
   var j = num;var a = 11; // just to enter the while loop at least once
   var count = 0;
while ( a > 10){
   a = num.toString().split('').map( e=> parseInt(e)).reduce((a,c)=> a*c);
   num = a;
   count++;
   j--; 
   }  
return count;
}}

Comments

0

You decremented your num by 1 each iteration. It gives you 29 because 39 - 10 is 29. You didn't update the actual num which you need to compare if it's still greater than 10.

Do this instead:

function test(num) {
  if (num < 10) {
    return num
  } else {
    var count = 0;
    while (num > 10) {
      num = num.toString().split('').map(e => parseInt(e)).reduce((a, c) => a * c);
      count++;
    }
    return count;
  }
}

console.log(test(39))

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.