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.
 

FAQ: Persistence bugger codewars kata

1. What is the "Persistence bugger codewars kata"?

The "Persistence bugger codewars kata" is a coding challenge on the Codewars platform that involves finding the multiplicative persistence of a number.

2. How does the "Persistence bugger codewars kata" work?

In this kata, you are given a number and you need to find the multiplicative persistence of that number. This involves multiplying all the digits of the number together, and then repeating the process with the result until you reach a single digit.

3. What is multiplicative persistence?

Multiplicative persistence is the number of times you need to multiply the digits of a number together until you reach a single digit. For example, the multiplicative persistence of 39 is 3 because it takes 3 multiplications (3*9=27, 2*7=14, 1*4=4) to reach a single digit.

4. What is the goal of the "Persistence bugger codewars kata"?

The goal of the "Persistence bugger codewars kata" is to write a function that calculates the multiplicative persistence of a given number and returns the result.

5. How can I improve my solution to the "Persistence bugger codewars kata"?

To improve your solution to this kata, you can try optimizing your code for efficiency, handling edge cases, and testing your function with different inputs to ensure its accuracy and robustness.

Similar threads

Replies
7
Views
1K
Replies
15
Views
2K
Replies
1
Views
1K
Replies
2
Views
1K
Replies
11
Views
4K
Replies
12
Views
1K
Back
Top