最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Multiplicative Persistence Codewars Challenge - Stack Overflow

programmeradmin1浏览0评论

I've been working on a kata from Codewars, the challenge is to write a function, persistence, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit.

Example:

persistence(39) === 3 // because 3*9 = 27, 2*7 = 14, 1*4=4
                       // and 4 has only one digit

persistence(999) === 4 // because 9*9*9 = 729, 7*2*9 = 126,
                        // 1*2*6 = 12, and finally 1*2 = 2

persistence(4) === 0 // because 4 is already a one-digit number

While trying to figure this out I came across a solution online (shown below) and after trying to understand its logic, I couldn't see why the code didn't work

var count = 0;

function persistence(num) {
  if (num.toString().length === 1) {
    return count;
  }
  count++;
  var mult = 1;
  var splitStr = num.toString().split("");
  for (var i = 0; i <= splitStr; i++) {
    mult *= parseFloat(splitStr[i])
  }
  return persistence(parseFloat(mult));
}

The output for any single digit number will be 0 which is correct however for any number that is multiple digits, the persistence always logs as 1 and I can't seem to figure out why, any help would be greatly appreciated.

I've been working on a kata from Codewars, the challenge is to write a function, persistence, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit.

Example:

persistence(39) === 3 // because 3*9 = 27, 2*7 = 14, 1*4=4
                       // and 4 has only one digit

persistence(999) === 4 // because 9*9*9 = 729, 7*2*9 = 126,
                        // 1*2*6 = 12, and finally 1*2 = 2

persistence(4) === 0 // because 4 is already a one-digit number

While trying to figure this out I came across a solution online (shown below) and after trying to understand its logic, I couldn't see why the code didn't work

var count = 0;

function persistence(num) {
  if (num.toString().length === 1) {
    return count;
  }
  count++;
  var mult = 1;
  var splitStr = num.toString().split("");
  for (var i = 0; i <= splitStr; i++) {
    mult *= parseFloat(splitStr[i])
  }
  return persistence(parseFloat(mult));
}

The output for any single digit number will be 0 which is correct however for any number that is multiple digits, the persistence always logs as 1 and I can't seem to figure out why, any help would be greatly appreciated.

Share Improve this question edited Feb 18, 2019 at 21:17 kish-an asked Feb 18, 2019 at 20:58 kish-ankish-an 751 silver badge6 bronze badges 8
  • If you're asking why the second piece of code acts as it does, it would be better not to confuse the issue by showing the other code. Also, both pieces of code use a for loop, neither show a while.... – Heretic Monkey Commented Feb 18, 2019 at 21:01
  • 1 Also, the code as posted results in something different, since count is only set to 0 the first time it is run... – Heretic Monkey Commented Feb 18, 2019 at 21:04
  • 1 The second function is bad, it depends on global variable count, so it gives different result according to when it was called. It should be rewritten to return 0 and return 1 + persistence(...) and not use global count at all. – Roman Hocke Commented Feb 18, 2019 at 21:06
  • I came across a solution proposed here on Stack Overflow - a link would probably be useful. Also, you are asking why somebody else's code doesn't work? – Matt Burland Commented Feb 18, 2019 at 21:09
  • @HereticMonkey Apologies, I posted the wrong piece of code but I've now removed it as I can see why it would be confusing. – kish-an Commented Feb 18, 2019 at 21:10
 |  Show 3 more ments

4 Answers 4

Reset to default 2

The posted code has quite a few problems.

for (var i = 0; i <= splitStr; i++) {

But splitStr is an array, not a number; i <= splitStr doesn't make sense. It should check against splitStr.length instead of splitStr.

Another problem is that it should use i <, not i <=, else the final splitStr[i] will be undefined.

Another problem is that the count variable is global, so more than one call of persistence will result in inaccurate results. There's no need for a count variable at all. To fix it:

function persistence(num) {
  if (num.toString().length === 1) {
    return 0;
  }
  var mult = 1;
  var splitStr = num.toString().split("");
  for (var i = 0; i < splitStr.length; i++) {
    mult *= parseFloat(splitStr[i])
  }
  return 1 + persistence(parseFloat(mult));
}

console.log(
  persistence(999),
  persistence(39),
  persistence(4)
);

Or, one could avoid the for loop entirely, and use more appropriate array methods:

function persistence(num) {
  const str = num.toString();
  if (str.length === 1) {
    return 0;
  }
  const nextNum = str.split('').reduce((a, b) => a * b, 1);
  return 1 + persistence(nextNum);
}

console.log(
  persistence(999),
  persistence(39),
  persistence(4)
);

Better try this i pleted Persistent Bugger kata by this script.

function persistence(num) {
  let count = 0;
  while(num.toString().length > 1){
    num = num.toString().split('').reduce((acc,el)=>acc *= +el,1)
    count++
  }
  return count
}

or we can use while loop with reduce array method

const persistence=(num)=>{

  let splitNumArr=num.toString().split('')
  let newList
  let count=0
  while(splitNumArr.length>1){

     newList=splitNumArr.reduce((acc,curr)=>{
        return acc*=curr
     })
      splitNumArr=newList.toString().split('')
      count++

  }

return count
}



console.log(persistence(39))===3
console.log(persistence(999))===4
console.log(persistence(9))===0
function persistence(num) {
   var times = 0;
   
   num = num.toString();
   
   while (num.length > 1) {
     times++;
     num = num.split('').map(Number).reduce((a, b) => a * b).toString();
   }
   
   return times;
}
发布评论

评论列表(0)

  1. 暂无评论