Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/examples/2_Math_Adding_Numbers/js/lesson.js
blob: 09999d8644298d163ea8f89f29e19750936f4a80 (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
163
164
165
166
167
168
169
/***** Check the browser resolution and modify css accordingly ******/
var myWidth;
var myHeight;

//alert(myWidth + ":" + myHeight);

function detect_and_adjust_browser(){
	if( typeof( window.innerWidth ) == 'number' ) { 
		//Non-IE 
		myWidth = window.innerWidth;
		myHeight = window.innerHeight; 

		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { 
		//IE 6+ in 'standards compliant mode' 
		myWidth = document.documentElement.clientWidth; 
		myHeight = document.documentElement.clientHeight; 

		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { 

		//IE 4 compatible 
		myWidth = document.body.clientWidth; 
		myHeight = document.body.clientHeight; 

		}
$("#imgBackGround").css({ width: myWidth-50, height: myHeight-135});
$("#footerSection").css({ width: myWidth-50, height: 50});
}
/*****   ****/

/**** Global Variables Declaration *****/
var GAME_LEVEL = 1;  //store the level of the game
var randNumbers = [];  //store the two random questions
var randOptions = [];  //store the five random answers including one correct
var optOtherPos = [];   //store the random positions including the right one
var optPosition = [];   //store the correct positions for the answers
var correctPosition;
var pos;
var correctAnswer;
correctCounter = 0;   //store the score of the correct Answer in interval of 5
					   // level is up at 5 correct Answers
var totalCounter = 0;  //
var flag_correct = 1;   //if flag=1, correct at 1st attempt
var NUM_LEVELS = 5;
var selectedOpt;


/***** Methods Declaration and definition ***/
function checkOption(optionSelected){
	pos = optPosition[optionSelected];
	var selectedOpt = randOptions[pos];	
	if(selectedOpt === correctAnswer){
		
		if(flag_correct == 1){   //correct at first attempt
				correctCounter++; 
		}
		totalCounter++;
		$("#scoreBox").html(correctCounter);
		$("#totalBox").html(totalCounter);
		if(correctCounter == 5*GAME_LEVEL){  //Hurray level is up
			GAME_LEVEL++;
		}
		if(GAME_LEVEL > NUM_LEVELS){
			//game over
			game();  //restart the game
		}
		next_numbers();
	}
	else{
		flag_correct = 0;
	}
}

function randomnumber(num1, num2)    //generate random number between any two ranges
{
	var num1 = parseInt(num1);
	var num2 = parseInt(num2);
	var generator = Math.random()*(num2-num1);
	generator = Math.round(num1+generator);
	return generator;
}


function get_random_position()	{                //generate random number include 0
	var rand_no = Math.floor(5*Math.random());
	return rand_no;
}
function generate_game_numbers(){	
	randNumbers[0]=randomnumber((GAME_LEVEL-1)*5,GAME_LEVEL*5);    
	randNumbers[1]=randomnumber((GAME_LEVEL-1)*5,GAME_LEVEL*5);
	correctAnswer = randNumbers[0]+randNumbers[1];
	randOptions[0] = correctAnswer; 
	for(i=1; i<5; i++){
		do{
			flag = 0;
			randOptions[i] = randomnumber((GAME_LEVEL-1)*10,GAME_LEVEL*10);
			for(j=0; j<i; j++){
				if(randOptions[i]===randOptions[j]){
					flag++;
				}
			}
		}while(flag != 0 );  //end of do while loop	
	}

	
	
	correctPosition = get_random_position();
	
	optOtherPos[0] = correctPosition;
	
	for(i=1; i<5; i++){
		do{
			flag = 0;
			optOtherPos[i] = get_random_position();
				for(j=0; j<i; j++){   //chek repeat within optOtherPos array
					if(optOtherPos[i] === optOtherPos[j]){
						flag++;
					}
				}
			
		}while(flag != 0);
			
	}

	for(i=0; i<5; i++){
		var pos = optOtherPos[i];
		optPosition[i] = optOtherPos[pos];
	}
	
	
}
function next_numbers(){
	flag_correct = 1;
	generate_game_numbers();	
	document.getElementById("number1").innerHTML = randNumbers[0];
	document.getElementById("number2").innerHTML = randNumbers[1];
	for(var i=0; i<5; i++){
		var x = i+1;
		pos = optPosition[i];
		document.getElementById("option"+x).innerHTML = randOptions[pos];
	}
	
		
}

function game(){
	correctCounter = 0;
	totalCounter = 0;
	GAME_LEVEL = 1;	
	$("#scoreBox").html(correctCounter);
	$("#totalBox").html(totalCounter);
	next_numbers();
}


/***** DOM READY *******/
$(document).ready(function(){
	$(window).bind("resize", detect_and_adjust_browser);
	$('#plussign').hide();
	$("#linkStart").click(function(){
		$('#plussign').show();
		if(totalCounter === 0){
			game();
		}
	});

	$("#linkPlayAgain").click(function(){
		game();
	});
});