Copy link to clipboard
Copied
I'm trying to calculate something and I'm having trouble understand calculations.
example:
why is
var s1 = 4;
var x = 1;
for ( var sum = s1; sum<=s1; sum++)x*=sum; result is 20
and why if used like this:
var s1 = 5;
var x = 1;
event.value = x*=sum; result is 5;
whats in "for" that is changing result?
In my case 20 is correct, but why in that case x*=sum is 20 and not 5?
The way I see it, to get 20 as result is because of for loop
for ( var sum = s1(5)-s2(2)+1=4; at this point sum is 4 so x(1)*sum = 4
now in the next loop since sum is now 5, now x(4)*sum(5)= 20.
and in the next loop sum is 6 and condition is sum<=s1(5) so there is no more loops so result is 20.
Copy link to clipboard
Copied
What want you calculate?
for ( var sum = s1; sum<=s1; sum++)x*=sum;
is the same as
x *= s1;
Copy link to clipboard
Copied
Sorry I tried to simplify formula. Since sum =s1(4) sum is = s1 so sum++ now sum is 5, 1 *5 is 5, so why when used in for loop it is 20?
Copy link to clipboard
Copied
I don't know where does you get the 20.
When I execute this in the Javascript console I get following:
var s1 = 4;
var x = 1;
for ( var sum = s1; sum<=s1; sum++)x*=sum;
x;
4
Copy link to clipboard
Copied
Ok, sorry about that I thought it will work if var are numbers but it looks like it only works if it gets value from field:
Copy link to clipboard
Copied
This is a other for loop!
Try the script of your first posting.
When you use a other script you will get also a other result.
Copy link to clipboard
Copied
Thank you Bernd for your help, I want to understand why is result 20.
Copy link to clipboard
Copied
The result of your first script is not 20
Copy link to clipboard
Copied
The way I see it, to get 20 as result is because of for loop
for ( var sum = s1(5)-s2(2)+1=4; at this point sum is 4 so x(1)*sum = 4
now in the next loop since sum is now 5, now x(4)*sum(5)= 20.
and in the next loop sum is 6 and condition is sum<=s1(5) so there is no more loops so result is 20.