Basic Algorithm Scripting: Factorialize a Number Help

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:

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

How to make it correct?

This will never reach 0 or 1, so your recursion never ends.

You need to make the input smaller.

1 Like

how do you calculate the factorial of a number?

1 Like

For example :
!4 = 4 * 3 * 2 * 1

1 Like

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

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. :grinning_face_with_smiling_eyes: 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 :grinning:
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