Aaronbob49
Aaronbob49

Reputation: 33

Factorialize a number using Javascript arrays

I tried making a function that takes a parameter, and puts every number less then the parameter up to zero and pushes it into an array(descending order). and then used a for loop to multiply each index of the array by the next index, but my return comes back null. please help.

function factorialize(num) {
     var arrayOfFactorial = [];
     var factorial = 1;
     for(var i = num;i > 0;i--){
        arrayOfFactorial.push([i]);
        factorial = factorial * arrayOfFactorial[i];
     } 

     return factorial;
}

factorialize(10);

Upvotes: 0

Views: 425

Answers (5)

baao
baao

Reputation: 73251

I think using a while loop makes it easier, and you don't need to create an array.

function factorialize(num) {
    let res = 1;
    while (num > 0) res *= num--;
    return res;
};

console.log(factorialize(10));

If you insist on using an array, a version using es6 spread operator could be

function factorialize(num) {
  return [...Array(num).keys()]
    .reduce((a, b) => a * (b + 1), 1)
}

console.log(factorialize(10));

Upvotes: 1

Debabrata
Debabrata

Reputation: 498

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

    console.log( factorial);
    return factorial;
}

factorialize(10);

i think this is what you are trying to do..i changed the [i] to i.and arrayOffactorial[i] to i

Upvotes: 0

Loïc Faure-Lacroix
Loïc Faure-Lacroix

Reputation: 13600

The array in you case isn't used for the calculation. What you were trying to do is probably this:

function factorialize(num) {
   var arrayOfFactorial = [];

   // Push all values in the array (order not really important here)
   for(var i = num;i > 0; i--){
     arrayOfFactorial.push(i);
   }

   var factorial = 1;
   for(var i = 0; i<arrayOfFactorial.length; i++) { 
     factorial = arrayOfFactorial[i] * factorial 
   }

   return factorial;
}

console.log(factorialize(10));

That said, there is no real point to do that as your memory usage will grow with the size of the factorial you're trying to compute and you don't really need to keep reference of everything. The best way would be to compute it by multiplying the factorial directly in the for loop. Like this:

function factorialize(num) {
   var factorial = 1

   for(var i = num;i > 0; i--){
     factorial = factorial * i
   }

   return factorial;
}

Upvotes: 0

kkica
kkica

Reputation: 4104

1-You are pushing an array into arrayOfFactorial, and then trying to multiply a number with an array. While it is legit in maths, it is not in javascript. You should use arrayOfFactorial.push(i);

2-You don't need an array. You can just use factorial = factorial * i

3-According to your explanation, You should use 2 loops. The second action, should be in a different loop.

4-You return something, but you do not print it.

See the Dij's answer if you can not fix the problem.

Upvotes: 0

Dij
Dij

Reputation: 9808

in the first iteration, arrayOfFactorial will be of length 1, and i will be equal to num which will be more than 1, you are trying to access arrayOfFactorial[num] which will be undefined and that is why you get NaN. you can just multiply factorial with i. Also you don't really need another array for this.

function factorialize(num) {
   var arrayOfFactorial = [];
   var factorial = 1;
   for(var i = num;i > 0;i--){
     arrayOfFactorial.push([i]);  //can be removed.
     factorial = factorial * i;
   } 

   return factorial;
}

console.log(factorialize(10));

Upvotes: 2

Related Questions