Script for making a text grid numerical summing to a specific value

From Catglobe Wiki
Revision as of 06:32, 8 October 2009 by Catglobe (talk | contribs) (Challenge)
Jump to: navigation, search

Challenge

In order to validate the inputs of a text grid question
As a questionnaire creator
I want to check if the sum of all values is equal to a specific value

Code

 1 /*
 2 In this example, we check if the total value equals to 100 or not.
 3 */
 4 
 5 function specialQuestionCheck()
 6 {
 7    var msg = "";
 8    var val = 0;
 9    for(i = 0; i < quest.questions.length; i++)
10    {
11       var tmpVal = document["query"][quest.questions[i].label].value;
12       if(tmpVal == "")
13       {
14          // NOPI
15       }
16       else if(isNaN(tmpVal))
17       {
18          msg += "- Angiv venligst et  tal\n";
19       }
20       else
21       {
22          val += parseFloat(tmpVal);
23       }
24    }
25 
26    if(msg == "")
27    {
28       if(val == 100)
29       {
30          return true;
31       }
32       else
33       {
34          alert("Værdierne skal summe til 100 - nuværende sum: " + val);
35          return false;
36       }
37    }
38    else
39    {
40       alert("Følgende fejl opstod:\n"+msg);
41       return false;
42    }
43    return false;
44 }
45 
46 function newGoNext()
47 {
48    document["query"]["dir"].value = "next";
49    return specialQuestionCheck();
50 }
51 
52 quest.next = newGoNext;