Difference between revisions of "Script for making a text grid numerical summing to a specific value"
m (→Challenge) |
m (→Code) |
||
Line 9: | Line 9: | ||
In this example, we check if the total value equals to 100 or not. | In this example, we check if the total value equals to 100 or not. | ||
*/ | */ | ||
− | + | var expectedTotal = 100; | |
function specialQuestionCheck() | function specialQuestionCheck() | ||
{ | { | ||
Line 33: | Line 33: | ||
if(msg == "") | if(msg == "") | ||
{ | { | ||
− | if(val == | + | if(val == expectedTotal) |
{ | { | ||
return true; | return true; | ||
Line 39: | Line 39: | ||
else | else | ||
{ | { | ||
− | alert("Værdierne skal summe til | + | alert("Værdierne skal summe til "+ expectedTotal +" - nuværende sum: " + val); |
return false; | return false; | ||
} | } |
Revision as of 06:34, 8 October 2009
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 var expectedTotal = 100;
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 == expectedTotal)
29 {
30 return true;
31 }
32 else
33 {
34 alert("Værdierne skal summe til "+ expectedTotal +" - 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;