-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
87 lines (71 loc) · 2.62 KB
/
Copy pathscript.js
File metadata and controls
87 lines (71 loc) · 2.62 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
/*
Tell user game has started when they click game start
Solve edge case where someone plays game without starting
What if someone puts a letter
Textbox should clear old value after submits
Solve game should end after user wins
Fix all the bugs
*/
// A GUESS GAME : collects a users guess;
// generate a random number b/wa spec. limit
// compares users guess with the random number
// all of this within 6 trials
// // if the user guesses correctly, the game ends
//if user guesses wrong , it should say if very bhigh or very low
// if usrr guesses wrong after 6 trials, game ends
//BONUS: There should be a try again
//There should also be a UI
const MAX_TRIES = 6;
let RANDOM_GUESS = 0;
let user_trial = 1;
let gameStarted = false;
let gameOver = false;
function startButton() {
const randomNumber = Math.round(Math.random() * 100);
RANDOM_GUESS = randomNumber;
console.log(`Random number: ${RANDOM_GUESS}`);
gameStarted = true;
gameOver = false;
document.querySelector('.message').innerText = 'Game started!!!. Make your guess';
document.querySelector('.user-guess').value = '';
document.querySelector('.input-section').classList.remove('hidden');
document.querySelector('.extra-options').classList.add('hidden');
document.body.style.backgroundImage = '';
}
function takeUserGuess(){
if(!gameStarted || gameOver){
return;
}
let userGuess = parseInt(document.querySelector('.user-guess').value);
const message = document.querySelector('.message');
console.log(`user guess: ${userGuess}`);
console.log(`User trial: ${user_trial}`);
if (userGuess == RANDOM_GUESS) {
message.innerHTML = "🎉🎉CONGRATULATIONS, You WIN!! 🎉 \nGame has ended";
gameOver = true;
return;
} else if (userGuess < RANDOM_GUESS) {
message.innerHTML = "TOO LOW, Please Try Again";
} else {
message.innerHTML = "TOO HIGH, Please Try Again";
}
user_trial++;
if (user_trial > MAX_TRIES){
showExtraOptions();
document.querySelector('.message').innerHTML = `Sorry, you lost. The answer was ${RANDOM_GUESS}`;
}
document.querySelector('.user-guess').value = '';
}
function showExtraOptions(){
gameOver = true;
document.querySelector('.extra-options').classList.remove('hidden');
}
function quitGame(){
gameOver = true;
document.querySelector('.input-section').classList.remove('hidden');
document.querySelector('.extra-options').classList.add('hidden');
}
function restartGame(){
startButton();
user_trial = 1;
}