0

why isnt counter counting past 2

function persistence(num) { var x, y, product = 1,counter = 0, product2 = 1 /*Product of numbers if first set does not result in solution */; x = num.toString().length; //length of inputed number y = num; //The inputed Number //Check the Number if (x > 0){ console.log('Continue'); y= y.toString(); for(var i = 0; i < y.length; i++ ){ product *= y[i]; } p = product.toString().length; //Length of product p2 = product2.toString().length; // length of product after iteration counter++; if(p == 1){ ; console.log('Ive Found the Multiplicative persistant Edward, it is: ' + counter); return counter; } else{ while(p != 1){ counter++; product = product.toString(); for(i = 0; i < p; i++){ product2 *= product[i]; } if(p2 == 1){ console.log('Ive Found the Multiplicative persistant Edward, it is: ' ); return counter; } } } } else('Not Enough Digits To determine') } var output = persistence(239); console.log(output);

23rd Oct 2018, 11:27 PM
Edward Jackson Jr
Edward Jackson Jr - avatar
2 odpowiedzi
+ 1
Maybe counter++ belongs in the for loop
24th Oct 2018, 5:52 AM
Anna
Anna - avatar
0
Edward There are several problems with your code- p and p2 are not updated as it should in the while loop product2 should be reset to 1 everytime the code is iterated and once the for loop inside of the while loop is finished product should equal product2 [product = product2]. But the main problem is you are overthinking this. Multiplicative Persistence - Multiply all the digits of a number n by each other, repeating with the product until a single digit is obtained. The number of steps required is known as the multiplicative persistence. https://code.sololearn.com/W01vYga9U986/#js https://code.sololearn.com/Wk9TGLNAM1W2/#js
24th Oct 2018, 7:03 AM
ODLNT
ODLNT - avatar