Basic Algorithm - Factorialize a Number

Hey, I’m looking for some input on my code for this challenge in Basic Algorithms - Factorialize a Number, I passed the test, but my code seems to be very different then most others; using the while format. Am I doing this in a weird way? Thanks in advance for the comments!

function factorialize(num) {
  var x = 1;
  var y = 1;
  while (x <= num) {
    y = y * x;
    x++;
  }  
  return y;
}

factorialize(5);

It’s not weird at all.

You can of course eliminate the need for this x variable, by using num in the following way:

Instead of counting up to num with x, decrement num itself and compare to 0.

For loops and while loops can be written to accomplish the same tasks. Your solution is not weird, just slightly different.

FYI - Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

1 Like

You don’t have to blur out your code (unless you just want to), because it is not a full solution.

Ahh right, good to know

Thanks Randell, I’ll keep this in mind for future posts; and thanks for the comment!

Thanks, I like eliminating the x variable, I’ll give it a shot using your solution, thanks for the help!