# Calculate

One of our intrepid KoBoNauts had a great question, and a solution worth sharing with the KoBo Users group.

The question is about checking answers. It demonstrates how to use the Calculate function in a constraints.

The scenario is one in which an enumerator is recording a number of responses and grading them as Correct or Incorrect. At the end of a set of these, the enumerator is asked how many Correct responses? We want to get the enumerators count and compare it to the actual number of correct answers. If the count is wrong, we want to show an error message.

In the set of questions, you have to adjust the values assigned to the options so that if correct=1 and incorrect=0. Note that KoBoForm usually assigns incremented values starting from 1 instead of 0 when auto-numbering. This is problematic because in form where the values for Correct=1 and Incorrect=2; if you add them up, the sum of getting two questions correct is the same as the sum of getting 1 question incorrect. It is literally a case where two rights make a wrong.

This would work better if correct=1 and incorrect=0. We may make a change in KoBoForm to accommodate incremented values starting at 0 instead of 1.

I have created an example form demonstrating how this calculation would work. It is attached.

You will see that there is an added node in the nodeset. This node does not have a question associated with it. That is because the form is doing the math. That math is contained in the BIND.

It adds up the values reported by 4 questions and stores the result in the node . Because I have edited the values for the options to be a 0 if the question is Incorrect and a 1 if the answer is Correct, we can then ask the final question, “How many questions were correct?” and compare that to the value in with a constraint. If it doesn’t match, we show an error message.

Try the attached form and you will see how it works. The constraint message uses a ‘.’ to mean ‘this question’. So it says, “THIS question must be equal to the value stored in ; if it is not, then show the error message.”

Calculate is a powerful tool in Xforms logic. You may find other uses, and the KoBo Users would love to see any great ideas you have.

Good luck,

~Neil Hendrick
KoBo Developer.

Corrects.xml (4.1 KB)