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
**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
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
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.
@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).
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!