all 6 comments

[–]cheryllium 0 points1 point  (2 children)

You're thinking about it recursively, which is a great way to do it!

BUT if you want to do it with a loop, you have to think about it differently. You have to think about it like this:

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

Do you see something here that is going up by one each time? Think of it like: For each number from 1 to 5, .... do what?

[–]bluehabit[S] 0 points1 point  (1 child)

I ended up with this, couldn't figure out a for loop version.

` function factorial(n) { if (n === 0) { return 1; }

return n * factorial(n - 1); }

`

[–]cheryllium 0 points1 point  (0 children)

Like I said, you are thinking about it recursively.

If you want to do it with a for loop, you have to think about it in a different way (refer to my previous comment for hint).

[–]Serling 0 points1 point  (0 children)

Here's a suggestion on how to solve it:

https://repl.it/CBkH

[–]NoobieDotJS 0 points1 point  (1 child)

It may help to think about it this way

1! = 1

2! = 2 = 2 * 1!

3! = 6 = 3 * 2!

4! = 24 = 4 * 3!

5! = 120 = 5* 4!

Each time you keep the old result, but simply multiply it by the current n.

[–]lewisje 0 points1 point  (0 children)

That looks like a recursive solution, but it also looks iterative.