-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaththis.js
More file actions
110 lines (79 loc) · 2.22 KB
/
Copy pathmaththis.js
File metadata and controls
110 lines (79 loc) · 2.22 KB
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
window.onload = init;
function init(){
var displayBox = document.getElementById("displayBox");
var userInputBox = document.getElementById("userInputBox");
var answerButton = document.getElementById("answerButton");
var startB = document.getElementById("startB");
var list_ul = document.getElementById("list_ul");
//startB.onclick = startGame;
answerButton.onclick = checkAnswer;
startB.onclick = startClicked;
function startClicked(){
var score = userInputBox.value;
var li = document.createElement("li");
li.innerHTML = score;
list_ul.appendChild(li);
saveScore(score);
}
displayBox.value = "";
userInputBox.value = "";
console.log("initiated");
startGame();
loadScore();
function startGame(){
displayBox.value = "";
userInputBox.value = "";
console.log("game started");
n1 = getRandomNumber();
n2 = getRandomNumber();
console.log("n1 is " + n1 + " and " + " n2 is " + n2);
displayBox.value = n1 + " + " + n2 + " =";
}
function getRandomNumber(){
var x = Math.floor((Math.random() * 10) + 1);
return x;
}
function checkAnswer(){
console.log("check answer button was clicked");
var userAnswer = userInputBox.value;
if (userAnswer == ""){
alert("Enter Your Answer Please");
} else {
var correctA = parseInt(n1 + n2);
console.log(n1 + " + " + n2 + " = " + userAnswer + " CORRENCT A IS: " + correctA);
if (userAnswer == correctA){
alert("correct")
} else {
alert("WRONG!");
}
}
startGame();
}
function saveScore(item){
var scoreArray = getStoredArray("scorelist");
scoreArray.push(item);
localStorage.setItem("scorelist", JSON.stringify(scoreArray));
}
function loadScore() {
var scoreArray = getSavedScore();
if (scoreArray != null){
for (var i = 0; i < scoreArray.length; i++){
var li = document.createElement("li");
li.innerHTML = scoreArray[i];
list_ul.appendChild(li);
}
}
}
function getSavedScore() {
return getStoredArray("scorelist");
}
function getStoredArray(key){
var scoreArray = localStorage.getItem(key);
if (scoreArray == null || scoreArray == ""){
scoreArray = new Array();
} else {
scoreArray = JSON.parse(scoreArray);
}
return scoreArray;
}
}