Question on Basic Algorithm Scripting - Factorialize a Number

Tell us what’s happening:

Hello everyone,

I’ve solved this exercise. but prior to solving it, I made a small mistake and play around with the code that cause it not working properly. I would like to understand why the incorrect code work that way.

so the incorrect code is:

function factorialize(num) {
  let x = 1;
  for(let i = 2; i <= num; i++){
   x *= num;
  }
  return x;
}

console.log(factorialize(5));
//->125

as you can see, the result is not correct. 5! should be 120 as it’s 5*4*3*2*1.I’m wondering why it shows 125. when I input another number for example factorialize(4) it returns 16. so what is num value in x *= num?

second question is regarding the correct code which is

function factorialize(num) {
    let x = 1;
    for(let i = 2; i <= num; i++){
     x *= i;
    }
    return x;
  }

console.log(factorialize(5));
//->120

I don’t understand how in this code factorialize(0) will return 1.
based on my understanding, as this line

for(let i = 2; i <= num; i++)

shows that i is 2. so when num is 0, i value will stay 2. Therefore x =1 multiplied by 2, shouldnt it be 2?
seems i misunderstood this.

kindly explain this 2 questions please :blush:

  **Your browser information:**

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

Challenge: Basic Algorithm Scripting - Factorialize a Number

Link to the challenge:

1 Like

Did you try logging the results:

function factorialize(num) {
  let x = 1;
  for(let i = 2; i <= num; i++){
   console.log(x)
   x *= num;
  }
  return x;
}

console.log(factorialize(5))

You get 125, when you execute factorialize 4 bc it runs 1*5*5*5 which is 125.

For the second code, the loop never runs (i<=num), so you are just returning the original definition of x, i.e x=1

Good questions, mind that I had to console.log(myVariable) to understand what was happening. It is normally quite useful.

1 Like

@Habushu Actually you explanation itself contains the answer. See that when you use

x *= num

What ever value of i, x will always multiply by, num which is not what you need.
But in the correct solution it has

x *= i

Hope you understood. Remember, if you don’t understand something, a quick console.log() will always help you :upside_down_face:

thanks! now that you point that out, I reliazed that it didn’t fulfill the arguments and therefore skip the loop and only return x;.

but for the incorrect code, I still don’t get it.

factorialize 4 will return 16 not 125. factorialize 5 will return 125. Therefore, I don’t understand what value of num is.

I understand this. my question is, what is the value of num when x*=num.

I did use console.log() as you can see on the post. However, I’m yet to understand how to use console.log() to show in some section that I don’t understand not only the final. It would be great if you could teach me how to do that :smiley:

You can toss console.log() anywhere, and it will show up in the console so long as that code is run. So if you ever toss a console.log() somewhere and expect it to show some information but its not, its in a section of code that is never being run. I often put a descriptive string in a log just to see if portions of my code are running when i expect them to be. The values you use in it will also have to be in scope, otherwise you will get an error. So toss a console.log() in your for loop with num in there like this console.log(num) and see what it says.

2 Likes

update:

@mahneh @isurumaldeniya first, I’d like to make some correction. the incorrect code, when we write factorialize(5) will return 625 and when factorialize(4) is written it will return 64.
Now, I think I understand how the incorrect code works, after understanding how factorialize(0) will return 1 in the correct code.
Please correct me if I’m wrong.

I found out that when:

function factorialize(num) {
  let x = 1;
  for(let i = 2; i <= num; i++){
   x *= num;
  }
  return x;
}

console.log(factorialize(5));
//->625

factorialize(4) will return 64
factorialize(5) will return 625
factorialize(6) will return 7776

it’s because when the code run for:
factorialize(4) what happen is 1*4*4*4
factorialize(5) what happen is 1*5*5*5*5
factorialize(6) what happen is 1*6*6*6*6*6

so why did the code run like that is because the loop works in a sense that x will multiply “i <= num times” with the value of num because we wrote x*=num. (for example if num is 4, that means from 2 to 4 there are 2,3,4 which mean in total there are 3 numbers. so x will multiply num by 3 times).

ah make sense, that’s why it didn’t shows when i put the console.log(num) after everything. Thank you!

Hi,
value of the num does not change what ever you do to the x since num is an argument you provide to the function, and you are not doing anything to it inside the function. Hope this helps you!

thank you, I’ve understood how the code works :slight_smile:

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