Conditionals in calculate, logic + syntax

So the problem is as follow. I have a set of ten select1 questions, with answer values of 1, 2, 3, 97, 98
I then need to calculate a sum of the following type of sequence:
If a user has entered 1 twice in a row, each 2 that follows (without being interrupted)
If 1, 1, 2, 1, 2, 1, 1, 2, 2, 2 then calc would be 4. I'm thinking of something like:

if val1 == 1 && val2 == 1
  if val3 == 2
    then count++
    if val4 == 2
      then count++
      if val5 == 2
        then count++
        if val6 == 2
          then count++
          if val7 == 2
            then count++
            if val8 == 2
              then count++
              if val9 == 2
                then count++
                if val10 == 2
                  then count++
if val2 == 1 && val3 == 1
  if val3 == 2
    then count++
    if val4 == 2
      then count++
      if val5 == 2
        then count++
        if val6 == 2
          then count++
          if val7 == 2
            then count++
            if val8 == 2
              then count++
              if val9 == 2
                then count++
                if val10 == 2

etc

I think this captures everything without double counting? I'm not sure on the syntax for ODK tho...

I can't help with the double counting, but this is the kind of thing that might be easiest to do after you get the data and not during form entry.

As far as a strategy to do this on the device, you should use calculations to store values. I'd break it up into multiple calculations so it's easier to debug with ${var} outputs.

And the various operators you'll use are at https://opendatakit.org/help/form-design/binding and https://opendatakit.github.io/xforms-spec. The key ones will be:

  • if(condition, a, b)
  • selected(xpath/to/node, value)
1 Like

K, thanks Yaw. I'll push the PI to do this type of calculation during data analysis... I agree that it makes sense there.