-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (84 loc) · 2.63 KB
/
index.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
let randomNum = Math.floor((Math.random() * 100) + 1)
const submit = document.querySelector('#subt')
const userInput = document.querySelector('#guessField')
const guessSlot = document.querySelector('.guesses')
const remainGuess = document.querySelector('.lastResult')
const lowOrHi = document.querySelector('.lowOrHi')
const startOver = document.querySelector('.resultParas')
const p = document.createElement('p')
let prevGuess = []
let numGuess = 1
let playGame = true
if(playGame){
submit.addEventListener('click', (e) => {
e.preventDefault()
const guess = parseInt(userInput.value)
validateGuess(guess)
})
}
function validateGuess(guess){
// validates the user input; valid input is given or not
if(isNaN(guess)){
alert('Please enter a valid guess!!')
}
else if(guess < 1 || guess > 100){
alert('Number should be between 1 to 100')
}
else{
prevGuess.push(guess)
if(numGuess >= 10){
displayGuess(guess)
displayMessage(`Game Over!! NO more guesses!!`)
endGame()
}
else{
displayGuess(guess)
checkGuess(guess)
}
}
}
function checkGuess(guess){
// checks the guessed number is correct or not according to that user gets something
if(guess === randomNum){
displayMessage(`Bravo!! You guessed the number 🥳`)
endGame()
}
else if(guess < randomNum){
displayMessage(`You are halfway there ;)`)
}
else if(guess > randomNum){
displayMessage(`You got further buddy :(`)
}
}
function displayMessage(message){
// displays the message for the user regarding the game info
lowOrHi.innerHTML = `<h2>${message}</h2>`
}
function displayGuess(guess){
userInput.value = ''
guessSlot.innerHTML += `${guess}, `
numGuess++;
remainGuess.innerHTML = `${11 - numGuess}`
}
function newGame(){
const newGameButton = document.querySelector('#startGame')
newGameButton.addEventListener('click', (e) => {
randomNum = Math.floor((Math.random() * 100) + 1)
prevGuess = []
numGuess = 1
guessSlot.innerHTML = ''
remainGuess.innerHTML = `${11 - numGuess}`
userInput.removeAttribute('disabled')
startOver.removeChild('p')
playGame = true
})
}
function endGame(){
userInput.value = ''
userInput.setAttribute('disabled', '') // disables the input to take further input from the user
p.classList.add('button')
p.innerHTML = `<h2 style = "cursor : pointer;" id = "startGame">Start a New Game</h2>`;
startOver.appendChild(p);
playGame = false
newGame()
}