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

From Catglobe Wiki
Revision as of 04:21, 8 October 2009 by Catglobe (talk | contribs) (Created page with '== Challenge == You want to make sure that the total value of a numerical text grid equal to a specific value == Code == <source lang="javascript" line="1" > /* In this example, …')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Challenge

You want to make sure that the total value of a numerical text grid 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   {
 8 
 9     var msg = "";
10 
11     var val = 0;
12 
13  
14 
15     for(i = 0; i < quest.questions.length; i++)
16 
17     {
18 
19       var tmpVal = document["query"][quest.questions[i].label].value;
20 
21  
22 
23       if(tmpVal == "")
24 
25       {
26 
27          // NOPI
28 
29       }
30 
31       else if(isNaN(tmpVal))
32 
33       {
34 
35         msg += "- Angiv venligst et  tal\n";
36 
37       }
38 
39       else
40 
41       {
42 
43          val += parseFloat(tmpVal);
44 
45       }
46 
47     }
48 
49     if(msg == "")
50 
51     {
52 
53       if(val == 100)
54 
55       {
56 
57          return true;
58 
59       }
60 
61       else
62 
63       {
64 
65           alert("Værdierne skal summe til 100 - nuværende sum: " + val);
66 
67           return false;
68 
69       }
70 
71     }
72 
73     else
74 
75     {
76 
77        alert("Følgende fejl opstod:\n"+msg);
78 
79        return false;
80 
81     }
82 
83     return false;
84 
85   }
86 
87  
88 
89   function newGoNext()
90 
91   {
92 
93     document["query"]["dir"].value = "next";
94 
95     return specialQuestionCheck();
96 
97   }
98 
99   quest.next = newGoNext;