Javascript for loop

function FirstFactorial(num) {

// code goes here
for(var i=num;i>=1;i–){

`````` var num1=num-1;
num=num1*num;
``````

}
return num;

}

// keep this function call here
FirstFactorial(5);

20741616380

Hey guys I am trying to get a factorial of a given number using for loop and function. I cant understand why the result is showing such a huge number. what kind of logical error is here.

I think the issue comes from you defining `num1` as `num - 1`, because every time the loop iterates, it redefines `num1` as the current `num` value minus one. After the first loop, num is equal to 20, which the code then reads as `20 * (20-1)`

there are multiple fixes to this:

1. You can define num1 outside of the for loop
2. You can use `i` to determine the size of num1 inside the loop
3. You can ditch the num1 variable altogether and just multiply `num` by `i`

Otherwise, your code works mostly as expected!

2 Likes

The decrement-shortcut is 2 minus-signs like
`i--`
copy-ing and testing your code gave an extended minus-sign !! being ascii 196 ─
instead of the required twice ascii 45 - (might be a transmission error…wireless, Phone or language-setting)

You could use some console.log()'s to get an idear what is going on
like

``````function FirstFactorial(num) {

// code goes here
for(var i=num;i>=1;i--){
console.log("========")
var num1=num-1;
num=num1*num;
console.log(num1,num)
}
return num;
}
// keep this function call here
FirstFactorial(5);

``````

and as the FirstFactorial() function is using the RETURN statement, giving a result with NODISPLAY
you might want use
`console.log( FirstFactorial(5) );`

2 Likes

The large number is a product of the loop you’ve built. Here are the numbers for each loop to help you out:

Loop 1 → `num = (5 - 1) x 5 = 20 `
Loop 2 → `num = (20 - 1) x 20 = 380`
Loop 3 → `num = (380 - 1) x 380 = 144,020`
and so on.

Add this line of code after the `num=num1*num;` to display what is happening in your loop:

``````// display what is happening in the loop.
console.log(i, num1, num);
``````

Why?

1. Compute with a range of numbers starting at the incoming down to 1.

With a factorial of 5!, we want to build a function that does the following 5 x 4 x 3 x 2 x 1. Right? You have the numbers being decremented within the `for` loop via `i`. Did you notice that when you ran the above `console.log()`? It prints out 5, then 4, then 3, then 2, and then 1.

Think about that. Isn’t that what you want to do the equation?

1. Compute a result of the product of the current number, which is in `i` x the current result.

Look at your code:

Notice that you’re assigning it back to the original variable of `num`. What is the value of `num` when you first start the loop?

What if instead, you create a new variable before the loop, e.g. `var result = 1;`. Hmm, then you keep assigning the product to `result`. And when you’re done, you return the factored result.

Try that.

2 Likes

Thanks mam !!! Its my 2nd day for learning coding. Even though i completed the basics it took 15 min for me to build this logic and still wrong . I dont know is that only me or every programmer go through this phase. People say you must be genius to be good at coding…

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