0

an you please help me optimize the code structure?

I have a piece of code as shown below. The entire code contains three 'switch' statements, but the 'alert' reminder no longer works after running. Is it not possible to have 'switch' statements nested within other 'switch' statements? Can you please help me optimize the code structure? Thank you.

if ZM = 0 and LM = 0 then

alert(---

"No!"

---)

else

switch 'L&Z' do

case 1:

(

let ;

if ZM < 'quantity' then

alert(---

"No!"

---)

else

if ZM >= 'quantity' then

switch '1' = '2' do

case true:

(

let;

let;

);

alert(---

"Yes!"

---);

end

)

case false:

(

alert(---

"No!"

---);

)

end

end

end

end

)

case 2:

if ZM = 0 and LM < 'quantity' then

alert(---

"No!"

---)

else

if LM >= 'quantity' then

let;

switch '1' = '2' do

case true:

(

let;

let;

);

alert(---

"Yes!"

---);

end

)

case false:

(

alert(---

"No!"

---);

)

end

end

else

if LM < 'quantity' and ZM != 0 then

let;

let;

let;

if  then

let;

let;

alert(---

"Ok!"

---)

);

end

end

end

end

end

end

end

end

10 replies

null
    • Fred
    • 6 mths ago
    • Reported - view

    Let me see if I read you code properly:

    check if ZM = 0 and LM = 0 

    if true than show NO in an alert box.

    if false switch on field 'L&Z'.

        if the value of L&Z = 1 then check if ZM is less than the value of quantity

        if true then show NO in an alert box.

        if false than check if the value of ZM is greater than or equal to value of quantity

            if true then do a switch when field '1' equals field '2'. (side note, you don't need this if statement since you in the else part of the one above. So if it fails ZM less than the value of quantity, then it must be greater than or equal to the value of quantity.)

            if true then show YES in an alert box.

            if false then show NO in an alert box.

        if the value of L&Z = 2 then check if ZM = 0 and LM is less than the value of quantity.

        if true then show NO in an alert box.

        if false then check if LM is greater than or equal to the value of quantity. (side note, again not needed since you are in the else part)

            if true then run a switch that checks if the value of field '1' equals to field '2'

            if true then show YES in an alert box.

            if false then show NO in an alert box.

    then you have a loose else statement (that checks if LM is less than the value of quantity and ZM does not equal 0) that I can't seem to find an if statement to go with.

    Does this code work?

    Are you copying this out of the code editor in Ninox? Does this code give no errors in the editor? In addition you have let statements that are empty.

      • gold_cat
      • 6 mths ago
      • Reported - view

       

          if ZM = 0 and LM = 0 then
              alert(---
      "No1!"
              ---)
          else
              switch '出库零整' do
              case 1:
                  if ZM < quantity then
                      alert(---
      "No2!"
                      ---)
                  else
                      if ZM >= quantity then
                          switch cnt(split(text(code), "
      ")) = quantity do
                          case true:
                              alert(---
      "Yes1!"
                              ---)
                          case false:
                              alert(---
      "NO3!"
                              ---)
                          end
                      end
                  end
              case 2:
                  if ZM = 0 and LM < quantity then
                      alert(---
      "No4!"
                      ---)
                  else
                      if LM >= quantity then
                          switch cnt(split(text(code), "
      ")) = quantity do
                          case true:
                              alert(---
      "Yes2!"
                              ---)
                          case false:
                              alert(---
      "Yes3!"
                              ---)
                          end
                      else
                          if LM < quantity and ZM != 0 then
                              alert(---
      "Yes4!"
                              ---)
                          end
                      end
                  end
              end
          end
      end
      

      Ninox didn't report any errors, but there was no response when I pressed the button. My previous code didn't include two 'switch cnt(split(text(code), " ")) = quantity do' statements, but after adding them, the problem occurred.

      • Fred
      • 6 mths ago
      • Reported - view

      with such a complicated set of ifs and switches you will need to start at the beginning and with a new formula field start with your first if then slowly add the switches and ifs one step at a time to find the where it break. You can remove the alerts and just leave the text. You also have to make sure the record also has data that will match.

      • gold_cat
      • 6 mths ago
      • Reported - view

       It seems that this is the only way. I want to confirm whether switch and if can always be used in combination as long as the syntax is correct.

      • gold_cat
      • 6 mths ago
      • Reported - view

       Continuous multilevel combinations.

    • John_Halls
    • 6 mths ago
    • Reported - view

    Try something like this

    let cr := "
    ";
    let a := switch true do
        case ZM + LM = 0:
        "No1!"
        case ZM + LM != 0 and 出库零整' = 1 and ZM < quantity:
        "No2!"
        case ZM + LM != 0 and 出库零整' = 1 and ZM >= quantity and cnt(split(text(code), cr)) = quantity:
        "Yes1!"
        case ZM + LM != 0 and 出库零整' = 1 and ZM >= quantity and cnt(split(text(code), cr)) != quantity:
        "No3!"
        case ZM + LM != 0 and 出库零整' = 2 andZM = 0 and LM < quantity:
        "No4!"
        case ZM + LM != 0 and 出库零整' = 2 andZM = 0 and LM >= quantity and cnt(split(text(code), cr)) = quantity:
        "Yes2!"
        case ZM + LM != 0 and 出库零整' = 2 andZM = 0 and LM >= quantity and cnt(split(text(code), cr)) != quantity:
        "Yes3!"
        case ZM + LM != 0 and 出库零整' = 2 andZM = 0 and LM < quantity and ZM != 0:
        "Yes4!"
    end;
    alert(---
    a
    ---)
    

    I am sure it can be massively simplified with a bit of thought.

      • gold_cat
      • 6 mths ago
      • Reported - view

       Thank you, John Halls, for the new idea!

      • John_Halls
      • 6 mths ago
      • Reported - view

       My pleasure. There are a few mistakes you will need to take care of, but hopefully, you get the idea.

      • Fred
      • 6 mths ago
      • Reported - view

       It seems like all your cases are exclusive, but remember to put the case that will happen the least often at the top and the cases that happen the most often at the bottom. Switches start from the top and finds the first case that evaluates as true.

      • gold_cat
      • 6 mths ago
      • Reported - view

       Thank you for this tip, I'm still learning, and it's very useful for me. I'm trying to think of all possibilities as much as I can at the moment. Writing code is really not easy.

Content aside

  • Status Answered
  • 6 mths agoLast active
  • 10Replies
  • 94Views
  • 3 Following