-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
99 lines (77 loc) · 2.33 KB
/
main.js
File metadata and controls
99 lines (77 loc) · 2.33 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
// random math number here for guess
var guessNumber ;
// how many guesses
var guessCount = 10;
// public variables
var txtinput = document.querySelector('.guessField')
var guessSubmit = document.querySelector('.guessSubmit');
var prev = document.querySelector('.guesses');
var last = document.querySelector('.lastResult');
var lh = document.querySelector('.lowOrHi');
var startNew = document.querySelector('button');
// event listners
guessSubmit.addEventListener('click',compareNumber);
startNew.addEventListener('click',startNewGame);
// load the form
startNewGame();
// function area
function compareNumber(){
// check is it a valid
if(isNaN(txtinput.value) || txtinput.value<1 || txtinput.value>100){
alert("Enter Correct Value");
txtinput.value ="";
txtinput.focus();
return;
}
// guesses are over
if(guessCount==0)
gotoStart();
// update guess count
guessCount--;
// show previous items
if(prev.textContent == '')
prev.textContent = "Previous guesses: ";
prev.textContent += txtinput.value + " ";
// is high value entered
if(guessNumber<txtinput.value){
last.textContent = "Wrong !";
last.style.backgroundColor ="Red";
lh.textContent = "Last guess was too high!"
}
// is low value entered
if(guessNumber>txtinput.value){
last.textContent = "Wrong !";
last.style.backgroundColor ="Red";
lh.textContent = "Last guess was too Low!"
}
// is equal value entered
if(guessNumber == txtinput.value){
last.textContent = "Congratulations! You got it right!";
last.style.backgroundColor ="Green";
lh.textContent = ""
// start new game
gotoStart();
}
txtinput.value ="";
txtinput.focus();
}
// the start position
function gotoStart(){
guessSubmit.disabled = true;
txtinput.disabled = true;
startNew.style.display = 'block';
}
function startNewGame(){
guessNumber= Math.floor(Math.random() * 100) + 1;
// set all as before
last.textContent = "";
last.style.backgroundColor ="White";
lh.textContent = "";
prev.textContent="";
// counts
guessCount = 10;
// enable and desable items
guessSubmit.disabled = false;
txtinput.disabled = false;
startNew.style.display = 'none';
}