forked from chetnagrover00/JavaScript-Mini-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
77 lines (70 loc) · 2.36 KB
/
script.js
File metadata and controls
77 lines (70 loc) · 2.36 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
var newgame = document.getElementById('newGame');
var newhead1 = document.getElementById('newhead');
var inputBox = document.getElementById('input1');
var prevGuess = document.querySelector('.guesses');
var guessRem= document.getElementById('remainingGuess');
var numGuesses = 1;
function startGame(){
var random1 = Math.round(Math.random()*100);
random = random1;
function compare(x,y){
if(x===y){
newhead1.classList.add('heading1');
newhead1.innerHTML="You Guessed Correctly!!";
inputBox.setAttribute('disabled', '');
newgame.classList.add('heading1');
newgame.innerHTML="Start New Game!";
}
else if (x < y){
newhead1.classList.add('heading1');
newhead1.innerHTML="Too low! Guess again!";
}
else if (x > y){
newhead1.classList.add('heading1');
newhead1.innerHTML="Too high! Guess again!";
}
}
function limitnum(){
newhead1.classList.add('heading1');
newhead1.innerHTML="Oh! You couldn't guess the right number!!! The real number was " + `${random}`;
inputBox.setAttribute('disabled', '');
newgame.classList.add('heading1');
newgame.innerHTML="Start New Game!";
}
function displayGuesses(){
numGuesses++;
guessRem.innerHTML = `${11 - numGuesses} `;
if (numGuesses === 11){
limitnum();
}
}
function myfunc(event){
event.preventDefault();
var input12 = inputBox.value;
input2=Number(input12);
if(input2===0){
}
if((input2<1 || input2>100) && input2 !== 0){
alert('please enter a valid number!');
}
else if(input2 !== 0){
compare(input2, random);
inputBox.value="";
prevGuess.innerHTML+=`${input2} `;
displayGuesses();
}
}
document.getElementById('button').addEventListener('click',myfunc);
}
startGame();
document.getElementById('newGame').addEventListener('click',function(){
startGame();
newgame.innerHTML="";
newgame.classList.remove('heading1');
newhead1.innerHTML="";
newhead1.classList.remove('heading1');
numGuesses = 1;
guessRem.innerHTML = `${11 - numGuesses} `;
prevGuess.innerHTML = "";
inputBox.removeAttribute('disabled');
})