JavaScript Persistence bugger codewars kata

  • Thread starter Thread starter shivajikobardan
  • Start date Start date
AI Thread 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 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
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

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