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

conditional functions

Participant ,
Sep 02, 2021 Sep 02, 2021

Copy link to clipboard

Copied

Hi
Can anyone help me on this formula?
var a1 = this.getField ("PURPOSE_1"). value;
var a2 = this.getField ("DURATION_1"). value;
var s1 = this.getField ("PRICE_1"). value;
var s2 = this.getField ("MORTGAGE_1"). value;
var s3 = s2 / s1;
if (s3 <= "0.5") {var t1 = "0.5";} else if (s3> "0.5" && s3 <= "0.6") {var t1 = "0.6" ;} else if (s3> "0,6" && s3 <= "0,7") {var t1 = "0,7";} else if (s3> "0,7" && s3 <= "0,8" ) {var t1 = "0,8";}
event.value = a1 + "" + a2 + "" + t1;
The division between the content of the "MORTGAGE_1" field and the content of the "PRICE_1" field if less than 0.5 must result in 0.5 if the result is greater than 0.5 and less than 0.6, the result must be 0.6 etc.
The result of the function of t1 is "undefined"
Can anyone help me to correct my mistake?

TOPICS
JavaScript

Views

340

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 ,
Sep 02, 2021 Sep 02, 2021

Copy link to clipboard

Copied

You get "undefined" because you can't divide by zero,check that 's1' is not 0 and then set 's3' division.

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 ,
Sep 02, 2021 Sep 02, 2021

Copy link to clipboard

Copied

Drop the quotes around the numbers in your code. When you add it they are treated as strings, not numbers.

Also, you can't use a comma as the decimal separator, only a period (when doing mathematical operations, that is, you can use it when you print out the numbers).

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
Participant ,
Sep 02, 2021 Sep 02, 2021

Copy link to clipboard

Copied

Hi. Thank you very much for the suggestion.
I made the changes you suggested:
if (s3 <= 0.5) {var t1 = 0.5;} else if (s3> 0.5 && s3 <= 0.6) {var t1 = 0.6;} else if (s3> 0.6 && s3 <= 0.7) {var t1 = 0.7;} else if (s3> 0.7 && s3 <= 0.8) {var t1 = 0.8;}
The result does not change even if the ratio of numerator to denominator changes. For example, if s2 = 100000, and s1 is 125000, the result in s3 is 0.8. Instead the result t1 is always equal to 0.5

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 ,
Sep 02, 2021 Sep 02, 2021

Copy link to clipboard

Copied

Post the full code, please. Also, where did you place it?

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
Participant ,
Sep 02, 2021 Sep 02, 2021

Copy link to clipboard

Copied

var a1 = this.getField ("PURPOSE_1"). value;
var a2 = this.getField ("DURATION_1"). value;
var s1 = this.getField ("PRICE_1"). value;
var s2 = this.getField ("MORTGAGE_1"). value;
var s3 = s2 / s1;
if (s3 <= 0.5) {var t1 = 0.5;} else if (s3> 0.5 && s3 <= 0.6) {var t1 = 0.6;} else if (s3> 0.6 && s3 <= 0.7) {var t1 = 0.7;} else if (s3> 0.7 && s3 <= 0.8) {var t1 = 0.8;}
event.value = a1 + "" + a2 + "" + t1;

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 ,
Sep 02, 2021 Sep 02, 2021

Copy link to clipboard

Copied

LATEST

Where did you place the code?

 

Also, move the definition of t1 outside the if-conditions, and give it a default value.

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 ,
Sep 02, 2021 Sep 02, 2021

Copy link to clipboard

Copied

You set conditions so t1 will be 'undefined' until you make calculation, after that if you delete values it will still show  0.5, because you set if s3 <=0.5 so even if field is empty or 0 it will show 0.5.

Also what should happen if value is higher then 0.8?

It looks like you want to round up result,so instead of conditions you should use Math.ceil().

 

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