+ 1
why does this return undefined. Solving a coding challenge
// Multiplicative Persistence Challenge function persistence(num) { var x, y, product; x = num.toString().length; //length of inputed number y = num; //The inputed Number //Check the Number if (x > 0){ console.log('Continue'); y.toString(); for(var i = 0; i < y.length; i++ ){ product *= y[i]; console.log(product); } return product; } else('Not Enough Digits To determine') } var output = persistence(23); console.log(output);
4 odpowiedzi
+ 8
Two issues: product isn't a number so you can't multiply with it and y isn't a string so you can't index through it. Add a value:
var x, y, product=1; // Line 3
Save the string:
y=y.toString(); // Line 10
+ 2
So I need to set a value before I use them?
0
Yes, you need to set a value first, it's called initialization. Also, may I suggest using 'let' instead of 'var', as 'var'?
0
y.toString() //line 10 not define y, as new value
y = y.toString() // need to