Factorial Question

Hi all:

Don’t really know where to put this one, so mods please move if it’s in the wrong place.

The factorial question on the link below appears to be incorrect. My code appears to work but it will not let me pass the question. Not only that, I click the ‘show solution’ button and Codecademy’s own answer doesn’t pass the test. I’ve noticed in their answer they do:

result *= i + 1 (I’m sure there shouldn’t be a plus one here).

For anyone who is interested here is the code that I was trying to pass:

const factorial = num => {
  let total = 1;

  for(i = num; i > 0; i--) {
    total *= i;
  }

  return total;
}

Any help is appreciated :slightly_smiling_face:

https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-ii/modules/fecp-practice-javascript-syntax-arrays-loops-objects-iterators/articles/fecp-javascript-practice-arrays-loops-objects-iterators

The tests that they use in some of the interactive articles can give cryptically generic failure reasons, even for working code.

In this case, you can pass the test with your solution if you explicitly use the let keyword in your loop.

const factorial = num => {
  let total = 1;

  for(let i = num; i > 0; i--) {
    total *= i;
  }

  return total;
}
2 Likes

Ah! An oversight on my part and a silly one at that, thank you very much. :blush:

It still begs the question however, the i + 1 in their solution is incorrect is it not? :thinking: