Tell us what’s happening:
I have 2 questions.
- I tried both the iterative and recursive ways of solving this. Between the two, the iterative way seems to be quicker to respond. So is there any benefit to the recursive way?
- In both cases, when I enter a large enough number, the function seems to return Infinity when I console.log the return value. How could I print the actual result?
Your code so far
function factorialize(num) {
// if(num===0){
// return 1;
// }else{
// let factorial = 1;
// for(let i=1;i<=num;i++){
// factorial *= i;
// console.log(i + ":" + factorial);
// }
// return factorial;
// }
if(num===0){
return 1;
}else{
return num*factorialize(num-1);
}
}
console.log(factorialize(1000));
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/factorialize-a-number