# help with calculation

Explorer ,
Mar 10, 2021 Mar 10, 2021

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?

TOPICS
JavaScript

Views

184

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Adobe Community Professional , Mar 11, 2021 Mar 11, 2021
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.

Likes

8 Replies 8
Mar 10, 2021 Mar 10, 2021

Copied

What want you calculate?

for ( var sum = s1; sum<=s1; sum++)x*=sum;

is the same as

x *= s1;

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Mar 10, 2021 Mar 10, 2021

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?

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Mar 10, 2021 Mar 10, 2021

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

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Mar 11, 2021 Mar 11, 2021

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:

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Mar 11, 2021 Mar 11, 2021

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.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Mar 11, 2021 Mar 11, 2021

Copied

Thank you Bernd for your help, I want to understand why is result 20.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Mar 11, 2021 Mar 11, 2021

Copied

The result of your first script is not 20

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Mar 11, 2021 Mar 11, 2021

Copied

LATEST

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.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Resources