JavaScript basic algorithm

Tell us what’s happening:

Good evening people! i am in serious need of help here,I have been trying this algorithm and i can’t seem to get it right. Can someone please tell me what is wrong with my code.

please please please Your help would be greatly appreciated
here is it!!

function factorialize(num) {
  var counter =0;
  for(let i=1; i<=num; i++){
    i *= counter;
  }
  return  counter;
}
      


// function factorialize(num) {
// return num;
// }

factorialize(5);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36.

Challenge: Factorialize a Number

Link to the challenge:

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 (’).

Just at a quick glance, you seem a little confused about which variable is holding the product, and which you should be returning.

 i *= counter;

Is that where you want to store your running product? You are telling it to multiply the current contents of i by counter and store that into i.

return num;

Is that what you want to return?

Arg sorry thats my mistake it shouldn’t be “num”, it should be “counter”.

Right, that solves one problem, the other problem is still here:

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

The variable i is your loop control variable - you have to be very careful messing with those in an active loop. It this case, it creates and infinite loop. But that’s OK, because that line needs to be changed anyway.

When I make those two changes, the code works.

Also, please don’t change code/text in previous posts in the thread - it creates confusion for people that try to read the thread later.

Thanks a lot for your feedback i really appreciate it.

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