parikshitmishra001 parikshitmishra001 - 2 months ago 7
Javascript Question

The return is not working (javascript)

The function is returning undefined why it's not returning the array length.even at the start of the code it's printing in the console but return is not working.

var resArr = [];
var p;

function persistence(num) {
resArr.push(num);
console.log(resArr);
console.log(resArr.length);
if (num > 10) {
var v = 1;
var x = num.toString();
var arr = [];
for (i = 0; i < x.length; i++) {
arr.push(x.charAt(i));
}
console.log(arr);
for (j = 0; j < arr.length; j++) {
var v = v * arr[j];
}
persistence(v);
} else {
return resArr.length - 1;
}
}

Answer

You're not returning in all cases.

Change

persistence(v);

to

return persistence(v);