Factorialize a number help!

Hello, I am on “Factorialize a number” and have trouble seeing why my code is not factorializing the number.

Here is my code:

function factorialize(num) {
  while (num > 1){
    var product = 1;
    product = product * num;
    num = num - 1;
  }
  return product;
}

factorialize(5)

;

Thanks!

You must declare var product = 1 outside of your while block, otherwise your return product is outside of scope.

fixed code:
function factorialize(num) {  
  var product = 1; 
  while (num > 1) {    
  product = product * num;    
  num = num - 1;  
  }  
  return product;
  }

factorialize(5);

1 Like

Actually, that’s not true.

Javascript hoists variable to the top.

So this:

function factorialize(num) {
  while (num > 1){
    var product = 1;
    product = product * num;
    num = num - 1;
  }
  return product;
}

is equivalent to:

function factorialize(num) {
  var product;
  while (num > 1){
    product = 1;
    product = product * num;
    num = num - 1;
  }
  return product;
}

The issue thesnowlizard was having was that product was reset to 1 with each iteration.
If you called factorialize(0) then product was declared but not initialized (not set to 1) and function returned undefined.

By moving initialization outside of the loop you fixed the issue but it has nothing to do with scope.

Does it make sense?

1 Like

Hello.
You can use recursion to factorialize, like this :

function factorialize(num){
    if(num <= 1) return 1;
    return (num * factorialize(num-1));
}

factorialize(5);
// return 120

2 Likes

Thank you for the solution, but can you explain to me how recursion works? I’m thinking in this instance it would be 5 * 4 and that would be returned. How is it that 120 gets returned? Thanks again!

1 Like

The principle is to “re-use” the same function until the “end” condition is reached.

The factorisation formulae is : 5! = 5*4*3*2*1 and the result is 120, and generally:
n! = n * (n-1) * (n-2) * (n-3) 2 * 1 .

In the function factorialize(num), we first test if the ‘end’ condition is reached i.e. : if(num<=1) and if true, we just return 1, else we recall the same function with (num-1) and return the product of (num * factorialize(num-1)).

factorialize(5) = 5 * factorialize(4); // => 5 * (4!); or 5*(24)
factorialize(4) = 4 * factorialize(3); // => 4 * (3!); or 4*(6)
factorialize(3) = 3 * factorialize(2); // => 3 * (2!); or 3*(2)
factorialize(2) = 2 * factorialize(1); // => 2 * (1!); or 2*(1)
factorialize(1) = 1; // the stop condition (num<=1) => return 1.

5! = 5 * 4!; and 4! = 4 * 3!; and 3! = 3 * 2!; and 2! = 2 * 1!; 1! =1;

The logs inserted by @rmdawson71 explains very well the recursion.
While the ‘stop’ condition is false (num>1), we just recall the same function with num-1. In the logs we can see that the computation done in inverse order :
num=5, num=4, num=3, num=2,
when num<=1: return 1 ; num=2 : return 2 ; … num = 5

=>The result is : 1x2x6x24=120

Sorry, for my poor English, I’m a French speaker ! :blush:

I understand the principle here, but was apparently trying to do something way more complicated than necessary. I feel foolish now for being proud of figuring out how to turn every positive number less than the argument into an array. Then started trying to figure out how to use shift/unshift and/or map to take the first two numbers in the array and multiply then replace them. Ugh.

I got this to pass, but still a couple things I’m not clear on. If I enter a much larger number like 320, it returns null. Is this just because of the behind the scenes coding on the site, or is something else wrong?

Also, why should 0 return 1 instead of 0?

var x= 1 ;
for(;num>0;num–){
x*=num;
}return x;
}
this is my idea.I am very happy to communicated with everyone .

You’re miles and miles past the max number JS can display at 320!. Max JS can display is 170! . Different engines handle it slightly differently, but most now return Infinity for anything bigger (. Depending on how you’ve written the code, it may timeout - especially if the solution is recursive. Youre saying you’re using map, so assuming you’re creating an array then manipulating it, which will kill perf. There’s a 5000ms timeout on the FCC tests, assuming that’s why there is a null response (?).

Hi, this is just a general question attached to this thread. I was able to figure this out with a little read-search-ask (only part I couldn’t figure out is why when you factorialize 0 is 1, but I guess that’s just a math thing I didn’t pay attention to in school)

Anyway, it seems there are always a number of different ways to solve a problem, just wanted an expert’s opinion on if my solution was any good as oppose to the other ways of solving it (i.e. https://medium.freecodecamp.org/how-to-factorialize-a-number-in-javascript-9263c89a4b38). How do you judge which solution is ‘better’ or more efficient? I thought code was going to be pretty straight forward but now I see it could also be subjective? Love to see what’s everyone’s take on this. Thanks!

function factorialize(num) {
if (num === 0)
return 1;
var factorializedNum = num;
for (var i = 1; factorializedNum > i; i++) {
num *= i;
}
return num;
}

factorialize(5);

1 Like

Error there, missing {

1 Like

hi! i have doubt regarding "return " . The return statement ends function execution and specifies a value to be returned to the function caller. So return (num * factorialize(num-1)); how is this code working?

No, there’s no error. The entire instruction is if(num === 0) return 1;, it’s the same than if(num === 0) {return 1;}. The {} are needed when the if(…) statement is followed by more than one instruction, like in this example :
if(num ===0){ instruction1; instruction2; .... instructionN; return 1; }

1 Like

return (num * factorialize(num-1)); is recursion
it’s means return the result of : num x the value returned by factorialize(num-1).
For example, if the value of num is 3, the return will be return (3 * factorialize(2)); witch is equivalent to return (3 * 2) because factorialize(2) will return 2.

The return statement in a function can return the value returned by a function call. In the case of recursion we just call the same function with an other value.
For example, for factorialize(5), this part of code is equivalent to return(5 x 4!) or return(5 x 4 x 3!) or return(5 x 4 x 3 x 2!)** or return(5 x 4 x 3 x 2 x 1). And in general, the definition of n! = n x (n-1)!

1 Like

Oh, thanks.

Never knew that was a thing :slight_smile:

https://nelsonblog.netlify.com/posts/Factorialize-a-Number/

This is a blog post about how I solved it! hope it helps :slight_smile: