Skip to main content
Inspiring
January 19, 2024
Answered

Switch statement for pdf js

  • January 19, 2024
  • 1 reply
  • 2612 views

I am not a correct output for the following script:

 

var floor == this.getField("Floor Location").value;
var storeys == this.getField("Storeys").value;
var zero, oneStorey, twoStoreys, none, B, M, S;

if (storeys == "One storey") {
  storeys = oneStorey;
}
if (storeys == "Two storeys") {
  storeys = twoStoreys;
}

if (floor == "B") {
  floor = B;
}
if (floor == "M") {
  floor = M;
}
if (floor == "2nd") {
  floor = S;
}

switch (true) {

  case ((storeys == twoStoreys) && (floor == B)):
    event.value = "17";
    break;
  case ((storeys == twoStoreys) && (floor == M)):
    event.value = "11";
    break;
  case ((storeys == twoStoreys) && (floor == S)):
    event.value = "1";
    break;
   case ((storeys == oneStorey) && (floor == B)):
    event.value = "11";
    break;
  case ((storeys == oneStorey) && (floor == M)):
    event.value = "1";
    break;
  default:
        event.value = floor;
        break;}
 
This topic has been closed for replies.
Correct answer Khaliquzzaman Katchi

You've made it unnecessarily complicated (and incorrect).

See attached.


Never mind I found the bug: the statement should be changed to 

"else if((storeys == "Two storeys") && (floor == "2nd"))
event.value = "1"

Thank you for putting me in the right track.

1 reply

Nesa Nurani
Community Expert
January 19, 2024

When you're assigning values to variables, you should use a single equal sign (=) instead of a double equal sign (==).

Why so many variables in this line: var zero, oneStorey, twoStoreys, none, B, M, S;

You don't use 'zero' or 'none' variable at all, you are setting floor variable to 'M' , 'B' or 'S' variable, but you didn't assign anything to them.

try67
Community Expert
January 19, 2024

Also, your use of the switch command doesn't make sense. Why not simply use if-else if?

Inspiring
January 19, 2024

Because the switch statement I used is equivalent and simpler to in my script use than many "if" and "else" which could be confusing and there are too many combinations to deal with. Anyhow I tried that approach also and it does not work either. I am uploading the file for your own analysis.

Kk