3

I've made a javascript that will give the factorial of all numbers inputted into the array (minus the first number in the array "0" which I had to put there for filler purposes to make the code run correctly)

fact = [0, 4, 2, 5, 3];
factorial = 1;
for (x = 1; x < fact.length; x++) {
for (i = 1; i <= fact[x]; i++) {
 factorial *= i;
 if (fact[x] === i) {
    console.log(fact[x] + "!" + " " + "=" + " " + factorial);
     factorial = 1;
   }
  }
 }``

Everything works correctly here is the console.log:

4! = 24

2! = 2

5! = 120

3! = 6

My question is how can I change variable x and i's initial value to 0 so I can remove my space holder "0" in my array without making everything outputted to 0 because currently if i set my variables to 0 to start and remove the space holder "0" in my array the equation becomes:

factorial *= 0;

which will make everything equal to zero and I don't want that! I am very sorry I am bad at wording my question thanks for the help!

3 Answers 3

3

jsFiddle Demo

You could replace the inner for loop with a while loop like this:

var fact = [4, 2, 5, 3];
for (x = 0; x < fact.length; x++) {
 var factorial = fact[x];
 var result = 1;
 while( factorial > 0 ){
  result *= factorial--;        
 }
 console.log(fact[x] + "!" + " " + "=" + " " + result);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Another way to do this would be to put your loop in reverse:

for (i = n; i > 0; i--) {
    fact *= i;
}

This avoids the issue with 0 in the loop. You start at the number and go backwards until you hit 1.

You could also simplify things a bit by splitting up your factorial generator function from the loop through your array:

function each (a, f) {
    var i = 0,
        r = [];

    for (i = 0; i < a.length; i++) {
        r.push(f(a[i]));
    }

    return r;
}

function factorial (n) {
    var i = 0,
        fact = 1;

    for (i = n; i > 0; i--) {
        fact *= i;
    }

    return fact;
}

var factorials = each([4, 2, 5, 3], factorial);

console.log(factorials.join(', '));

EDIT:

Fiddle here: http://jsfiddle.net/ToddT/Jy5Lp/

1 Comment

Nice. You can reuse factorial then in a library of functions for the next project
0

My question is how can I change variable x and i's initial value to 0 so I can remove my space holder "0"

Why would you change i to 0? It's got nothing to do with the fact array at all. Your worry is misplaced. Your code should work just fine if you remove the place holder 0:

fact = [4, 2, 5, 3];
factorial = 1;
for (x = 0; x < fact.length; x++) {
  for (i = 1; i <= fact[x]; i++) {
    factorial *= i;
    if (fact[x] === i) {
      console.log(fact[x] + "!" + " " + "=" + " " + factorial);
      factorial = 1;
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.