Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/examples/lessons/6_Maths_quickAddition/js/lesson.js
blob: 84fe3279eed672b93969f662aa293ff8fc818c9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
$(document).ready(function(){
	var k = Karma({
		audio: [{'name':'correct','file':'correct.wav'},
			{'name':'incorrect','file':'incorrect.wav'}
		]});
		  
	k.ready(function(){
		var i,j;
		var totalCounter;
		var currentQuestion;
		var TOTAL_QUES = 16;
		var randPositions = [];
		var numFst;
		var num2nd;
		var s = 0; var play = 0; var restart = 0;
		
		var checkTime = function(timePara){
		    if (timePara<10 )
		    {
			timePara="0" + timePara;
		    }
		    return timePara;
		};

		
		var startTimer = function(){
					s=checkTime(s);					
					$('#timerBox1').html(s);
					increaseTime();					
		};
		
		var increaseTime = function(){
		    if(play == 1){
				if(restart == 1){
				s = 0;
				}
			s++;
			s=checkTime(s);				
			$('#timerBox1').html(s);
			var t=setTimeout(function(){increaseTime();},1000);			
		    }
		};

		var genRandPosition=function (){correctCounter = 0;
		  	randPositions[0] = k.rand(0,TOTAL_QUES-1);
			for(i=1; i<TOTAL_QUES; i++){
					do{
						flag = 0;
						randPositions[i] = k.rand(0,TOTAL_QUES-1);
						for(j=0; j<i; j++){
							if(randPositions[i] === randPositions[j]){
								flag++;
							}
						}
					}while(flag != 0 );  //end of do while loop	
				}
		};
		
		var next_question = function (){			
				numFst = k.rand(10,99);
				num2nd = k.rand(10,99);	
				$('#calcSection').html('').append(numFst +' + '+num2nd+' = ');
				$('#calcSection').append('<input type="text" class="textBox" maxlength="3" />');
				foucs_blur();				
				$('.textBox').focus();		correctCounter = 0;
		};
		correctCounter = 0;
		function game_start(){			
			genRandPosition();
			play = 1;
			startTimer();
			next_question();		
		}
		
		function game() {
			totalCounter = 0;	
			s = 0; play = 0; restart = 0;
			$('#section').html('').append('<div id="left-side"></div>');
			$('#left-side').append('<div id="tv"></div>');
			$('#tv').append('<div id="tvLayer"></div>');
			$('#tvLayer').addClass('tvOff');
			$('#left-side').append('<div id="cupBoard"></div>');			
			$('#section').append('<div id="right-side"></div>');
			$('#right-side').append('<div id="borderWall"></div>');
			$('#borderWall').append('<div id="imgDisplay"></div>');			
			for(var i = 0; i< TOTAL_QUES; i++){
				$('#imgDisplay').append('<div id="img'+i+'></div>');
				$('#img'+i).addClass('default');				
			}			
			$('#right-side').append('<div id="calcSection"></div>');
			
			$('#help').hide();
			
		}
		game();
		var delay_nextQues = function(){
			document.delayForm.delayval.value = 1;
			next_question();
		};
		
		var delay_gameOver = function(){
			document.delayForm.delayval.value = 1;
			$('#calcSection').html('');
			play = 0;
			for(var i = 0; i< TOTAL_QUES; i++){
				$('#imgDisplay').append('<div id="img'+i+'></div>');
				$('#img'+i).removeClass('correct').addClass('default');				
			}
			$('#tvLayer').addClass('tvOn').append('खेल खत्तम।');
						
		};
	
		var check_answer = function(){	
			textVal = $('.textBox').val();
			if((numFst+num2nd) == textVal){
				totalCounter++;
				k.audio.correct.play();
				$('#img'+randPositions[totalCounter]).removeClass('default').addClass('correct');
				if(totalCounter === TOTAL_QUES){
					t=setTimeout(function(){delay_gameOver();},1000);
				}
				else{
					t=setTimeout(function(){delay_nextQues();},1000);
				}
			}
			else{
				k.audio.incorrect.play();
			}
		};
		
		function foucs_blur(){
			$('input[type="text"]')
			.focus(function() {
			    $(this).removeClass('incorrect').addClass("focus");
			})
			.blur(function() {
			    $(this).removeClass("focus");
			})
			.keypress(function(event) {
				if(event.which === 13){
					check_answer();
				}
				
			});
			
		}
		
		$('#linkStart').click(function(){
			game_start();
		});
	
		$('#linkPlayAgain').click(function(){
			game_start();		
		});
		$('#linkHelp').click(function(){
			$('#help').slideDown(2000);
		})
		.mouseout(function(){
			$('#help').slideUp(2000);
		});
	}); //end of k.ready
});	//end of document.ready