Factorialize a Number Challenge Stuck

I’ve been trying to figure out Factorialize a Number algorithm from the Javascript Algorhithms and Data Structures course and I’m kind of stuck. I understand hinted solutions but I don’t get why mine doesn’t work:

function factorialize(num) {

  for (let i = 1; i < num; i++) {
      num = num * i;
    }
  return num;

}
factorialize(5);

The console output is obviously failed test along with this massage : “Potential infinite loop detected on line 3. Tests may fail if this is not changed.” Which to be honest I don’t quite understand as the description of the challenge says that “Only integers greater than or equal to zero will be supplied to the function.” Even if you’d pass the 0 or -5 or whatever then the condition part of a for loop definition simply evaluates to false and the function should return unaltered value back.

hi there, it’s because num keeps growing in line 3:

num = num*i

so i will never be less than num, and the for lop will just keep running, since i<num condition will never be met

whoaaa didnt noticed thanks!!

Hi @skinnymage !

Welcome to the forum!

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.