Persistence bugger codewars kata

  • JavaScript
  • Thread starter shivajikobardan
  • Start date
In summary, The conversation discusses a code that calculates the number of times a given number must be multiplied by its digits until it reaches a single digit. The code is not passing some tests and the issue is identified as the function not returning the correct output. After fixing the issue, the code is able to accurately calculate the required number of multiplications.
  • #1
shivajikobardan
674
54
TL;DR Summary
kata not solved.
I'm solving this kata:
https://www.codewars.com/kata/55bf01e5a717a0d57e0000ec/train/javascript

JavaScript:
function persistence(n) {
  var str = n.toString();
  let finalproduct;

  if (str.length == 1) {
    return 0;
  }

  do {
    finalproduct = sop(str);
    str = finalproduct;
    str = str.toString();
  } while (str.length > 1);

  return finalproduct;
}

function sop(str1) {
  let product = 1;
  for (let i = 0; i < str1.length; i++) {
    product = product * Number(str1[i]);
  }

  return product;
}

persistence(4);
Here's my code but it is not passing few tests. Why's that?
ZNoy6yAJnRrC-jvZ4e9U4DT0bd3iT0uHwgrKdOc6iR-O-umVmA.png
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
Your function doesn't return what the problem asks for: the number of times you must multiply the digits in num until you reach a single digit.
 
  • #3
DrClaude said:
Your function doesn't return what the problem asks for: the number of times you must multiply the digits in num until you reach a single digit.
thanks fixed it.
 

Similar threads

  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
Replies
1
Views
657
  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
1
Views
899
  • Engineering and Comp Sci Homework Help
Replies
2
Views
970
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Programming and Computer Science
Replies
11
Views
4K
Back
Top