freeCodeCamp Challenge Guide: Factorialize a Number

freeCodeCamp Challenge Guide: Factorialize a Number
0

Is this by any chance feasible by looping the numbers up to <= num and then multiplying each element of the resulting array by num and summing it all?
If yes: how? If no: how much am I wrong and why?

Here is a simple solution using a while loop and it works fine. No need to use additional if statement for returning 1 when num = 0:

function factorialize(num) {
var a = 1;
while (num > 0) {
a *= num;
num–;
}
return a;
}

factorialize(0);

3 Likes

here is my solution

function factorialize(num) {
  var i;
  var factor=1;
    for (i=num;i>0;i--){
      
      factor*=i;
    }

return factor;
} 

factorialize(5);

solution provided here in first post suggest that function can call it self inside the function, am I right?

after some thought I realise that solution should be

function factorialize(num) {
  if (num < 0) {retun "error";}
  if (num === 0) { return 1; }
  return num * factorialize(num-1);
}

factorialize(5);

if input is negative value than it will result in “stack overflow”, isn’t it?

2 Likes

My code-


function factorialize(num) {
  var factorial=1;
  if(num<=0){
    return 1;
  }
  for(i=num; i>0; i--){
    factorial = factorial*i;
  }
      return factorial;
}

factorialize(5);

I finally got it after a week… :relieved:

1 Like

Very clean and simple solution. I like it. Its easy to understanding what its doing. I am still learning JS and i wish more solutions could be tis simple and straightforward

1 Like

I know this ins’t the best answer but I used almost the same logic and it worked, check it out :

function factorialize(num) {

var arr = [];

if (num == 0) {

return 1;

}

while ( num > 0) {

arr.push(num);
num--;

}

var newArr = arr.reduce(function (x,y){
return x*y;

});

return newArr;

}

So why is this “factorialize(0) should return 1.”?

looking at example

Factorials are often represented with the shorthand notation n!
For example: 5! = 1 * 2 * 3 * 4 * 5 = 120

Doesn’t this mean 0! = 0 = 0?

2 Likes

After I failed with loops I figured I should turn the number to an array and multiply the numbers. Clearly it’s a more complicated version, which took me like 45 minutes to figure out.

But it works. :grinning:

function factorialize(num) {
  
  if (num === 0) {return 1;}
  var numArray = Array.from({length: num}, (i, num) => num +1); //creates an Array from 1 to num
  
  var multiplyed = numArray.reduce(function(a,b) {
        return a * b;
    }); // multiplies each number
  
  return multiplyed;

}


factorialize(5);

I actually had the exact same approach (below) to create an array of all integers from num to 1, and then use reduce to multiply all those integers.

It’s not as elegant as the other correct answers, but it seems to work fine, yet isn’t accepted as an answer. Hmm…

var factArray = [];

function factorialize(num) {
  if (num == 0) {return 1;}
 
  for (myVar = num; myVar >= 1; myVar--) {factArray.push(myVar);}
  
  num = factArray.reduce(function(a,b) {return a * b;});
  return num;
}

factorialize(5);
2 Likes

Hello campers.I am figuring on how to paste my code here.Someone with an adea?highly appreciated.Thanks.

function factorialize(num) {

var factorial = num;

if(num === 0) {

return 1;

}
return num * factorialize(num - 1);
}

factorialize(5);

This solution also checks for 0:

function factorialize(num) {

for (var i = num - 1; i > 0; --i)
num *= i;
if (num == 0)
num = 1;
return num;
}

factorialize(0);

Hello, my function works (although it is more complicated than the posted answer), but it is not letting me advance to the next challenge.

Instead of just putting the answer and moving on, I want to understand why my code is not getting a “pass”.

Here is my code:

var array=[];

function factorialize(num) {

for (i=1; i<=num; i++){
array.push(i);
}
if (num===0){
return 1;
}
num = array.reduce(function(a, b) {
return a*b;
});
return num;
}

factorialize(5); //returns 120

Does anyone have insight on this?

Hope this help :wink:

function factorialize(num) {
var product =1;

if (num===0) { return 1;}

for (var i=1;i<num; i++) {

product = product * i;

}
return num* product;

}

factorialize(5);

Hi campers,I was teaching myself to post code here using back ticks.


function factorialize(num) {
  
  var factorial = num;
  
  if(num === 0) {
    
    return 1;
  }
  return num * factorialize(num - 1);
}

factorialize(5);
function factorialize(num) {
  newNum = 1;
  for(var i = 1; i<=num; i++){
    newNum *= i;
  }
  num = newNum;
  return num;
}

factorialize(0);

Not fancy, but it works. Recursion should be it’s own lesson!

//declare an array
var arr= [];
function factorialize(num) {
//check if num is 0
if(num==0){
return 1;
}
//add values to array
else{
for(var i=1; i<=num; i++){
arr[i]= i ; // arr=[1,2,3,4,5]
}
}
//use reduce method to multiply values of array and reduce to one value
num=arr.reduce(function(a,b){
return a*b;
});
return num;
}
factorialize(0);

Here is 3 type of solution:

1. With “if”

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

2. With “While loop”

function loopFactorial(num) {
var result = num;
while (num > 1) {
result = result * (num-1);
num–;
}
return result;
}

3. With “ForLoop”

function forLoopFactorial(num) {
var result=1;
for (var i = num; i>0; i–){
result *= i;
}
return result;
}

#codecademy.com #pluralsight.com :heart:

2 Likes

This answer is really elegant, thanks!

1 Like