Skip to main content
Participant
August 8, 2019
Answered

Script won't give a result. Trying to assign a value to a var with a switch function

  • August 8, 2019
  • 1 reply
  • 639 views

Not sure what I am doing wrong here, but I think I am close. I am trying to have a function find the smaller of either one field entered by the user of the form or another set of numbers based on a dropdown menu.

I am not sure where I am going wrong here, I think I am using the switch function incorrectly to assign a value to one of my variables, but I have no idea how to fix it:

var z = this.getField ("Line1");

var y = this.getField ("Dropdown3").value;

var x = (function(y) {

switch(y) {

          case "S":

               x = 39375

               break;

          case "MFJ":

               x = 78750

               break;

          case "MFS":

               x = 39375

               break;

          case "HH":

               x = 52750

               break;

     }

})

event.value = Math.min(z.value, x.value);

this.getField ("Line11")=event.value;

The cases are the different values of the dropdown. That notation has worked in other fields, but I have never tried to use it to assign a value to a variable, only to find the output of a field. Line1 is a field where a user enters a number.

What am I doing wrong? I am not very well versed in javascript.

This topic has been closed for replies.
Correct answer Bernd Alheit

Try this:

var z = this.getField ("Line1");

var y = this.getField ("Dropdown3").value;

var x = 0;

switch(y) {

          case "S":

               x = 39375;

               break;

          case "MFJ":

               x = 78750;

               break;

          case "MFS":

               x = 39375;

               break;

          case "HH":

               x = 52750;

               break;

     }

event.value = Math.min(z.value, x);

1 reply

Bernd Alheit
Community Expert
Bernd AlheitCommunity ExpertCorrect answer
Community Expert
August 8, 2019

Try this:

var z = this.getField ("Line1");

var y = this.getField ("Dropdown3").value;

var x = 0;

switch(y) {

          case "S":

               x = 39375;

               break;

          case "MFJ":

               x = 78750;

               break;

          case "MFS":

               x = 39375;

               break;

          case "HH":

               x = 52750;

               break;

     }

event.value = Math.min(z.value, x);

will1031Author
Participant
August 8, 2019

That worked, thank you!

Why do you have to define var x as 0 first if you don't mind me asking?

Bernd Alheit
Community Expert
Community Expert
August 8, 2019

When you want you can remove the zero.