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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...

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