JavaScript Practice: Arrays, Loops, Objects, Iterators - factorial exercice


i’m doing the factorial exercice and my code is different from the official solution but return the correct value.

Did i miss something?

const factorial = number => {
for (i = number - 1; i > 1; i–) {
number = number * i
return number

// return 720

thanks for you help.


Your solution works for some factorials but not all of them. The factorial of 0 is supposed ;to be 1, but your solution would return 0.

Also, although it isn’t required by JavaScript, another community member recently had trouble with the interactive article accepting their solution because they didn’t explicitly use the let keyword when initializing the i variable in their for loop. The test in the interactive article gave a generic syntax error when checking his solution, but the output was correct.

Ive managed to pass the first exercise’s test but saddly it’s the only one i passed. Even thought i love those kind of challenges it is very frustrating sometimes when my code works but it doesnt pass their test and i dont get any feedback! Would you help me and look at my solutions and see if you can detect whats wrong with them?

Sadly, my PRO membership ran out yesterday so I don’t have access to that interactive article to look up the requirements for each function until I can subscribe again. If you post more details about what each of the last two functions is supposed to do (the sample input and output they provide), then I’d be happy to double check.

The generic errors can be tough when the code itself works in these articles

Edit: @eloncararr one thing I do notice is that you aren’t using let, const, or var when declaring your newArr in each of the last two functions. That is likely it, but I can’t test it myself due to lack of access.

Thanks mate, you were right, i can’t belive i didn’t saw this before! :wink:

I was so stuck from the project no.1 so I was searching here at forum.
For some reason the ‘Solution’ didn’t solve it, it prints out wrong factorial function.
While your solution helped me a lot @badasonic , I’d like to share my solution here too, just so if anyone else is lost like I was could see!

function factorial(num) {
  let result = [];
  for (let i = num; i > 0; i--) {
  return result.reduce((num, i) => (num * i))


That’s it! I’m going back to solve the rest of the practice, good luck everyone! :smiley:

You can also use recursion. I know recursive solutions haven’t been talked about much but take a look at this solution.

function factorial(num){

  if(num === 0 || num === 1) return 1;

  else return factorial(num - 1) * num;


I would recommend iterating the steps of this code on paper to visualize what is going on.