forked from prashantkalokhe/Hactoberfest-2022-New
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
102 lines (94 loc) · 3.03 KB
/
app.js
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
//Generate a random number between 1 and 500
let randomNumber = parseInt((Math.random()*100)+1);
const submit = document.querySelector('#subt');
const userInput = document.querySelector('#guessField');
const guessSlot = document.querySelector('.guesses');
const remaining = document.querySelector('.lastResult');
const startOver = document.querySelector('.resultParas');
const lowOrHi = document.querySelector('.lowOrHi');
const p = document.createElement('p');
let previousGuesses = [];
let numGuesses = 1;
let playGame = true;
if (playGame){
subt.addEventListener('click', function(e){
e.preventDefault();
//Grab guess from user
const guess = parseInt(userInput.value);
validateGuess(guess);
});
}
function validateGuess(guess){
if (isNaN(guess)){
alert('Please enter a valid number');
} else if (guess < 1) {
alert('Please enter a number greater than 1!');
} else if (guess > 100){
alert('Please enter a number less than 500!')
} else {
//Keep record of number of attempted guesses
previousGuesses.push(guess);
//Check to see if game is over
if (numGuesses === 11){
displayGuesses(guess);
displayMessage(`Game Over! Number was ${randomNumber}`);
endGame();
} else {
//Display previous guessed numbers
displayGuesses(guess);
//Check guess and display if wrong
checkGuess(guess);
}
}
}
function checkGuess(guess){
//Display clue if guess is too high or too low
if (guess === randomNumber){
displayMessage(`You guessed correctly!`);
endGame();
} else if (guess < randomNumber) {
displayMessage(`Too low! Try again!`);
} else if (guess > randomNumber) {
displayMessage(`Too High! Try again!`);
}
}
function displayGuesses(guess){
userInput.value = '';
guessSlot.innerHTML += `${guess} `;
numGuesses++
remaining.innerHTML = `${11 - numGuesses} `;
}
function displayMessage(message){
lowOrHi.innerHTML = `<h1>${message}</h1>`
}
function endGame(){
//Clear user input
userInput.value = '';
//Disable user input button
userInput.setAttribute('disabled', '');
//Display Start new Game Button
p.classList.add('button');
p.innerHTML = `<h1 id="newGame">Start New Game</h1>`
startOver.appendChild(p);
playGame = false;
newGame();
}
function newGame(){
const newGameButton = document.querySelector('#newGame');
newGameButton.addEventListener('click', function(){
//Pick a new random number
randomNumber = parseInt((Math.random()*100)+1);
previousGuesses = [];
numGuesses = 1;
guessSlot.innerHTML = '';
lowOrHi.innerHTML = '';
remaining.innerHTML = `${11 - numGuesses} `;
userInput.removeAttribute('disabled');
startOver.removeChild(p);
playGame = true;
})
}
//Allow to restart game with restart button
//Change DIV to a form so it can accept the enter key
//NOTES:
//NaN != NaN