code is like that :)
function test3(num) {
if (num <= 9) {
return num;
}
let rest = 1;
while (num) {
rest = rest * (num % 10);
num = parseInt(num / 10);
}
if (rest <= 9) {
return rest;
}
return test3(rest);
}
debugger;
let output = test3(786);
console.log(output); // --> 0
i understand other logic but, i dont understand 'parseInt' how to work parseInt logic ? apparently i m already read mdn, and any stackover flow answer, can't understand well..
parseInt()
is being used here to convert a number that may have a fraction after the decimal point into the integer part.
It works because if the argument to parseInt()
isn't a string, it's first converted to a string, and then it parses an integer prefix from that. So parseInt(3.14)
is equivalent to parseInt('3.14')
, which returns 3
.
It's equivalent to Math.floor()
in this context.