Q: Using a while loop to split an integer into digits, then multiply digits together Attempting a codewars kate (Persistent Bugger) where you have to take an integer and multiply the digits together until the result is < 10 (the answer is the number of times it takes to get under 10). 
E.g 39 === 3 because:


*

*3*9 = 27,

*2*7 = 14,

*1*4 = 4 


My logic is to use a while loop, so while multipliedNum > 10, Split integer into individual digits, multiply digits together (each time increasing a count by 1), then if the result is < 10, returning the count.
My issue is, I'm not sure where I'm going wrong with this code, it's not returning anything:
function persistence(num) {
  let multipliedNum = num;
  let count = 0;

  while (multipliedNum > 10) {
    multipliedNum = (num + '').split('').reduce((previousValue, currentValue) =>
      parseInt(previousValue, 10) * parseInt(currentValue, 10));
    count += 1;
  }

  if (multipliedNum < 10) {
    return count;
  }
}

persistence(39);

Any clue as to where I'm going wrong would be great! (a bit of a JS beginner here)

A: You need a different check for the while loop, because you need to include 10 for multiplying.
Then you need the same variable for reducing and assingning. You could take num for it. It does not harm if you recycle the same variable for the same purpose.
Then take a simplified callback for reduce, parseInt is not necessary, because of the implicit type casting to number by using the multiplication operand *. This operator expects numbers and tries to cast the operands to numbers.
At the end just return the count - without a check, which is never false.


function persistence(num) {
    var count = 0;

    while (num > 9) {
        num = num.toString().split('').reduce((a, b) => a * b);
        count++;
    }
    return count;
}

console.log(persistence(39));



A: The main problem with the code is that, it is stuck in an infinite loop, because of the condition : 
while(multipliedNum > 10)

Because you keep on splinting num and assign that to multipliedNum , therefore multipliedNum is always greater than 10.
Try the following: 


function persistence(num) {
  let multipliedNum = num;
  let count = 0;

  while (multipliedNum > 10) {
    multipliedNum = (multipliedNum + '').split('').reduce((previousValue, currentValue) =>
      parseInt(previousValue, 10) * parseInt(currentValue, 10));
    count += 1;
  }
  if (multipliedNum < 10) {
    return count;
  }
}

console.log(persistence(39));



A: @daggett, if you prefer to use recursion then following code will also work for your problem but I do not recommend you to use this. 
I just presented to solve your problem using recursion which is a great concept used in large applications to simply the tasks.
Sample log for inputs 39 and 336
3 * 9 = 27
2 * 7 = 14
1 * 4 = 4
============
3 times


7 * 8 * 6 = 336
3 * 3 * 6 = 54
5 * 4 = 20
2 * 0 = 0
============
4 times

Code


function timeToGetNumberLessThan10(num) {
 var s = '' + num
 var chars = []

 if(s.length == 1)
  return 1

 sum = 1

 for(var i=0; i < s.length; i++) {
  chars.push(s[i])
  sum *= parseInt(s[i])
 }

 // Log message
 console.log(chars.join(' * ') + ' = ' + sum) 

 if(sum < 10) {
  return 1
 } else {
  // Recursive call
  return 1 + timeToGetNumberLessThan10(sum)
 }
}


// TEST 1
var count = timeToGetNumberLessThan10(39);
console.log('============')
console.log(count + ' times')

console.log('\n') // New line

// TEST 2
var count = timeToGetNumberLessThan10(786);
console.log('============')
console.log(count + ' times')

/*
3 * 9 = 27
2 * 7 = 14
1 * 4 = 4
============
3 times


7 * 8 * 6 = 336
3 * 3 * 6 = 54
5 * 4 = 20
2 * 0 = 0
============
4 times
*/



A: while (multipliedNum > 10) {
  multipliedNum = (num + '').split('').reduce((previousValue, currentValue) =>
    parseInt(previousValue, 10) * parseInt(currentValue, 10));
  count += 1;
}

You're assigning the result to multipliedNum but using num to start with, which means it keeps splitting the same num over and over. Change num to multipliedNum.
