JavaScript Persistence bugger codewars kata

  • Thread starter Thread starter shivajikobardan
  • Start date Start date
Click For Summary
The discussion revolves around a coding challenge from Codewars that involves creating a function to determine how many times the digits of a number must be multiplied together until a single-digit result is achieved. The initial code provided incorrectly returns the final product instead of the count of multiplications required. The user identifies the issue and acknowledges the need to modify the function to correctly track and return the number of iterations needed to reach a single digit. The conversation highlights the importance of understanding the problem requirements and ensuring the function's output aligns with those expectations.
shivajikobardan
Messages
637
Reaction score
54
TL;DR
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
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.
 
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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 7 ·
Replies
7
Views
1K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
Replies
55
Views
6K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K