Code challenge in JavaScript Practice: Arrays, Loops, Objects, Iterators

For the code challenge section of JavaScript Practice: Arrays, Loops, Objects, Iterators…

Is there any way to make a for loop for the factorial function? I am not seeing any online so wonder if I am going in the complete wrong direction? So far I have:

function factorial(num) {
  for (let i = num; i > 0; i--) {
    newNum = (num) * i;
  return newNum;

I am at a standstill so any help would be appreciated

1 Like

Interesting :thinking: never seen this before.

I figured this out and you are very close. With the line newNum = (num) * i you are just multiplying i with the number you inputted not the product of the previous iteration of the loop. Now with that do you see what you need to change for this to work. You also need to change what the comparison is in the loop.

Currently, this is what the computer is doing if we put 5 as the argument (120 is the right answer):

5 * 5 // outputs 25
5 * 4 //outputs 20
5 * 3 //outputs 15
5 * 2 //outputs 10
5 * 1 //outputs 5

But we need to multiple 5 * 4 then multiple the previous result by 1 less than the previous iteration. So the next one would be 20 * 3.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.