Tell us what’s happening:
Why I see in the output :
InternalError: too much recursion
My code so far
function factorialize(num) {
if (num === 0 || num === 1) {
return num;
} else {
return factorialize(num * num -1);
}
}
factorialize(5);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:83.0) Gecko/20100101 Firefox/83.0
.
Challenge: Factorialize a Number
Link to the challenge:
Learn to code. Build projects. Earn certifications.Since 2015, 40,000 graduates have gotten jobs at tech companies including Google, Apple, Amazon, and Microsoft.
ilenia
November 29, 2020, 4:33pm
#2
when the function is called as factorialize(5)
the return statement becomes return factorialize(5 * 5 - 1)
so return factorialize(24)
, inside which the return statement is return factorialize(24 * 24 - 1)
your numbers are going a bit too high, and never reaching 1 or 0
This will never reach 0 or 1, so your recursion never ends.
You need to make the input smaller .
1 Like
ilenia
November 29, 2020, 4:34pm
#5
how do you calculate the factorial of a number?
1 Like
For example :
!4 = 4 * 3 * 2 * 1
1 Like
ilenia
November 29, 2020, 4:36pm
#7
so 4! === 3! * 4
, right?
if you can translate that in javascript it will pass
2 Likes
I will try and check the code in the challenge
Can you give me a hint of how to do
ilenia:
3!
on the code editor?
Hi @abdulrahman.mhd.anas !
So the 3! is written as 3x2x1 right?
So couldn’t we rewrite it like this.
3 x factorial(2)
or even better
3 x factorial(3-1). Maybe we could incorporate this in our answer somehow.
Also be careful here
factorial(0) needs to return 1. Don’t ask me why that is. There was a whole discussion on it a few weeks back on the forum.
Thanks, but I mean how to write that in JavaScript
Well you have a parameter num. try to see if you could use num in the expression I gave you instead of hard coding in numbers
Hope that helps.
Now I know what you mean
Do you mean that:
else {
return num * factorialize(num - 1);
}
1 Like
Now you just have to make that small change to the if statement and it should pass