+ 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);

20th Oct 2018, 1:10 AM
Edward Jackson Jr
Edward Jackson Jr - avatar
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
20th Oct 2018, 1:54 AM
John Wells
John Wells - avatar
+ 2
So I need to set a value before I use them?
20th Oct 2018, 12:26 PM
Edward Jackson Jr
Edward Jackson Jr - avatar
0
Yes, you need to set a value first, it's called initialization. Also, may I suggest using 'let' instead of 'var', as 'var'?
20th Oct 2018, 12:42 PM
LunarCoffee
LunarCoffee - avatar
0
y.toString() //line 10 not define y, as new value y = y.toString() // need to
20th Oct 2018, 6:30 PM
Daniel Ustavshchikov