17. Counting Answers

With this session, you will learn how to count items with BrainJS, embedded JavaScript for NLP.

The Problem

Let's continue with the cells we created in the previous sessions.

Example 1.

  • Cell A
    • *
    • <strong/>what s in the classroom
    • I didn't found that in the classroom. Can you try again?
              <think><set-context>what s in the classroom</set-context></think>
  • Cell B
    • <nerve>itemInClassroom</nerve>
    • <strong/>what s in the classroom
    • Good! Next?
              <think><set-context>what s in the classroom</set-context></think>
  • Cell C
    • <nerve>itemInClassroom</nerve> *
    • <strong/>what s in the classroom
    • <goto><s>1</s></goto>
  • Cell D
    • * <nerve>itemInClassroom</nerve> *
    • <strong/>what s in the classroom
    • <goto><s>2</s></goto>
  • Cell E
    • * <nerve>itemInClassroom</nerve>
    • <strong/>what s in the classroom
    • <goto><s>2</s></goto>

They work like a charm in responding to student's answers. However there is a big problem: the Tutor doesn't know when to stop asking students questions. Even the students have listed all of the items in the classroom, the Tutor will keep asking for it endlessly.

The Solution

There are more than one ways to address the issue. In this session we will learn use counting to exit from the loop.

  • The Tutor counts the correct answers given by students.
  • She stops asking when the answers reach a threshold.

If you have any experience with a programing language, you know what we want to do is implement a loop.

  answerCount = 0;
  threshold = 3;
  while (answerCount < threshold) {
    ask for an answer;
    if (answer is correct){
      count ++;

To implement the above structure, we will use BrainJS. Firstly we define a property of custom user object to store the count.

  1. Click Codes tab.
  2. Click Add icon.
  3. Enter "answerCount" for the Name box.
  4. Click New property button.
  5. Enter 0 in the box right to = symbol.
  6. Click Save button.

The steps above define a property named answerCount for your custom user object, and set its default value as 0.

Then go back to edit the Cell B above.

  1. Click Cells tab.
  2. Click the pencil icon to edit Cell B.
  3. Click Advanced fieldset.
  4. Under Conditional Output, enter "user.answerCount > 2" between "if (" and ") then".
  5. Under the above condition, enter "Very good! We have finished Unit 1. Let's have a small talk."
  6. In Output field, insert the contents below within <think/>: <user>answerCount ++</user>.
  7. Click Save button.

You then get Cell B enhanced as below.

  • Cell B.1
    • <nerve>itemInClassroom</nerve>
    • <strong/>what s in the classroom
    • Conditional Output: if (user.answerCount > 2) then

      Very good! We have finished Unit 1. Let's have a small talk.
    • Good! Next?
                <set-context>what s in the classroom</set-context>
                <user>answerCount ++</user>

Together with other cells, Cell B.1 keeps asking students for answer until she gets 3 correct answers.

  • Log 3
    • What's in the classroom?
    • A picture.
    • You're right. Next?
    • Two blackboards.
    • Good job! More?
    • Many desks.
    • Very good! We have finished Unit 1. Let's have a small talk.
    • ...

Things to Keep in Mind

  • To count answers, you define a new property "answerCount" for custom user object under Codes tab.
  • To increase the count by one, place <user>answerCount ++</user> in the Output field, within <think/> or its value will be included with output.
  • To exit from the loop, use Conditional output.