Algorithm Scripting -- Factorialize a Number

Hey guys, I’ve been working on this problem for over an hour today. I know I’m getting close. In fact, my solution even works in Codepen. It just won’t pass the FreeCodeCamp tests. Can anyone help?

Codepen Link: https://codepen.io/camchardukian/pen/MLewQm

FreeCodeCamp Challenge Being Discussed:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/factorialize-a-number
Code sample:

var result = 1;
function factorialize(num) {
  for (var i = 1; i <= num; i++) {
  result*=i;
    }
  if (result!=0) {
  return result;}
  else {
    return 1;
  }
}

factorialize(5);
document.write(result);

You are using a global variable, as FreeCodeCamp call various times the tests, result change each time

Try adding more than one call and then print result to the console

An other thing you need to consider is
What happens with factorialize(0)

2 Likes