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

hello,

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
}

console.log(factorial(6))
// return 720

thanks for you help.

Hello,

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.

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