skip to Main Content

Trying to use nested switch commands to generate correct outputs on specific fields. In this specific example, the heading value of basement, main floor or 2nd floor of a house depends on the number of house floors. I tried 2 different scripts but none of them has provided any output.

//1st script:

switch (this.getField("Floor Location").value) {

  case "-":
    event.value = "-";
    break;

  case "B":
    switch ((this.getField("Storeys").value)) {
      case "-":
        event.value = "-";
        break;
      case "One Storey":
        event.value = "11";
        break;
      case "Two Storeys":
        event.value = "17";
        break;
    }

  case 'M':
    switch (this.getField("Storeys").value) {
      case "-":
        event.value = "-";
        break;
      case "One Storey":
        event.value = "1";
        break;
      case "Two Storeys":
        event.value = "11";
        break;
    }

  case "2nd":
    switch (this.getField("Storeys").value) {
      case "-":
        event.value = "-";
        break;
      case "One Storey":
        event.value = "_";
        break;
      case "Two Storeys":
        event.value = "1";
        break;
    }
  default:
    event.value = this.getField("Floor Location").value;
    break;
}

// No output

//2nd script:

var TwoStoreys = storeys;
var floor = this.getField("Floor Location").value;
if (floor = "-");
var none = floor;
var storeys = this.getField("Storeys").value;
if (storeys = "-");
var zero = storeys;
if (storeys = "One storey");
var OneStorey = storeys;
if (storeys = "Two Storeys");
var TwoStoreys = storeys;

var floor = this.getField("Floor Location").value;
if (floor = "-");
var none = floor;

if (floor = "B");
var B = floor

if (floor = "M");
var M = floor

if (floor = "2nd");
var Second = floor;

switch (true) {
  case ((zero) && (none)):
    event.value = "-";
    break;
  case ((zero) && (B)):
    event.value = "-";
    break;
  case ((zero) && (M)):
    event.value = "-";
    break;
  case ((zero) && (Second)):
    event.value = "-";
    break;
  case ((Onestorey) && (none)):
    event.value = "-";
    break;
  case ((Onestorey) && (B)):
    event.value = "11";
    break;
  case ((Onestorey) && (M)):
    event.value = "1";
    break;
  case ((Onestorey) && (Second)):
    event.value = "-";
    break;
  case ((Twostoreys) && (none)):
    event.value = "-";
    break;
  case ((Twostoreys) && (B)):
    event.value = "17";
  case ((Twostoreys) && (M)):
    event.value = "11";
    break;
  case ((Twostoreys) && (Second)):
    event.value = "1";
    break;
}

2

Answers


  1. 1 – First Script

    The lack of break statements:

    switch (this.getField("Floor Location").value) {
    
        case "-": 
            event.value = "-";
            break;
    
        case "B":
            (this.getField("Store").value) {
                case "-": 
                    event.value = "-";
                    break;
                case "One Storey":
                    event.value = "11";
                    break;
                case "Two Storeys":                           
                    event.value = "17";
                    break;
            }
            break; //ADDED
        
        case 'M':
            switch (this.getField("Storeys").value) {
                case "-": 
                    event.value = "-";
                    break;
                case "One Storey":
                    event.value = "1";
                    break;
                case "Two Storeys":
                    event.value = "11";
                    break;
            }
            break; //ADDED
         
        case "2nd":   
            switch (this.getField("Storeys").value) {
                case "-": 
                    event.value = "-";
                    break;
                case "One Storey":
                    event.value = "_";
                    break;
                case "Two Storeys":
                    event.value = "1";
                    break;
            }
            break; //ADDED
    
        default:
            event.value = this.getField("Floor Location").value;
            break;
    

    2 – Second Script

    The use of = instead of === on the second script while using conditional statements was the major error, so you only need to change this, but I made this for you:

    var floor = this.getField("Floor Location").value;
    var storeys = this.getField("Storeys").value;
    var zero, oneStorey, twoStoreys, none, B, M, Second;
    
    if (storeys === "-") {
      zero = storeys;
    } 
    if (storeys === "One storey") {
      oneStorey = storeys;
    }
    if (storeys === "Two Storeys") {
      twoStoreys = storeys;
    }
    
    if (floor === "-") {
      none = floor;
    }
    if (floor === "B") {
      B = floor;
    }
    if (floor === "M") {
      M = floor;
    }
    if (floor === "2nd") {
      Second = floor;
    }
    
    switch (true) {
      case ((zero === storeys) && (none === floor)):
        event.value = "-";
        break;
      case ((zero === storeys) && (B === floor)):
        event.value = "-";
        break;
      case ((zero === storeys) && (M === floor)):
        event.value = "-";
        break;
      case ((zero === storeys) && (Second === floor)):
        event.value = "-";
       break;
      case ((oneStorey === storeys) && (none === floor)):
        event.value = "-";
        break;
      case ((oneStorey === storeys) && (B === floor)):
        event.value = "11";
        break;
      case ((oneStorey === storeys) && (M === floor)):
        event.value = "1";
        break;
      case ((oneStorey === storeys) && (Second === floor)):
        event.value = "-";
       break;
      case ((twoStoreys === storeys) && (none === floor)):
        event.value = "-";
        break;
      case ((twoStoreys === storeys) && (B === floor)):
        event.value = "17";
      case ((twoStoreys === storeys) && (M === floor)):
        event.value = "11";
        break;
      case ((twoStoreys === storeys) && (Second === floor)):
        // Please set the expected output for this case
        event.value = "YOUR_EXPECTED_VALUE";
        break;
    }
    

    Other recommendations:

    1 – Group all declarations at the top of your code.

    2 – Avoid duplicate variable declarations.

    3 – Use === instead of == for comparison to avoid unintended type coercion.

    4 – Provide a default value in your switch statement.

    Login or Signup to reply.
  2. I’d use an object to map between the values and forgo switch altogether:

    const map = {
      B: {
        "-": "-",
        "One Storey": "11",
        "Two Storeys": "17"
      },
      "2nd": {
        "-": "-",
        "One Storey": "_",
        "Two Storeys": "1"
      }
    };
    
    let floorLocation = "B"; //this.getField("Floor Location").value;
    let storeys = "Two Storeys"; //this.getField("Storeys").value;
    
    let result = map[floorLocation]?.[storeys];
    if (typeof result === "undefined") {
      result = "-";
    }
    
    console.log(result);
    
    floorLocation = "2nd";
    storeys = "One Storey";
    
    result = map[floorLocation]?.[storeys];
    if (typeof result === "undefined") {
      result = "-";
    }
    
    console.log(result);
    
    floorLocation = "-";
    storeys = "-";
    
    result = map[floorLocation]?.[storeys];
    if (typeof result === "undefined") {
      result = "-";
    }
    
    console.log(result);

    Note: I use optional chaining so I don’t have to worry about whether a floor location is defined. I could also remove the "-" entries too, since everything defaults to "-" anyway.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search