Persistence bugger codewars kata

  • Context: JavaScript 
  • Thread starter Thread starter shivajikobardan
  • Start date Start date
Click For Summary
SUMMARY

The discussion centers on the Codewars kata titled "Persistence," which requires calculating how many times the digits of a number must be multiplied together until a single-digit result is achieved. The provided JavaScript function, `persistence(n)`, initially fails to meet the kata's requirements by not returning the correct count of multiplications. The solution was corrected by ensuring the function tracks the number of iterations needed to reach a single digit.

PREREQUISITES
  • Understanding of JavaScript functions and syntax
  • Familiarity with string manipulation in JavaScript
  • Knowledge of loops and conditionals in programming
  • Basic concepts of recursion and iterative processes
NEXT STEPS
  • Review JavaScript function return values and how to track state across iterations
  • Learn about the concept of recursion and how it can simplify problems like persistence
  • Explore Codewars challenges to practice similar algorithmic problems
  • Investigate performance optimization techniques for iterative functions in JavaScript
USEFUL FOR

JavaScript developers, coding challenge enthusiasts, and anyone looking to improve their problem-solving skills in algorithm design.

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.
 

Similar threads

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