• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

help with calculation

Explorer ,
Mar 10, 2021 Mar 10, 2021

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?

TOPICS
JavaScript

Views

448

Translate

Translate

Report

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

correct answers 1 Correct answer

Community Expert , 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.

Votes

Translate

Translate
Community Expert ,
Mar 10, 2021 Mar 10, 2021

Copy link to clipboard

Copied

What want you calculate?

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

is the same as

x *= s1;

 

Votes

Translate

Translate

Report

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

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?

Votes

Translate

Translate

Report

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

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

Votes

Translate

Translate

Report

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

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:

Izrezak.PNG

 

 

Votes

Translate

Translate

Report

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

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

The result of your first script is not 20

Votes

Translate

Translate

Report

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

Copy link to clipboard

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.

Votes

Translate

Translate

Report

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