This repository has been archived by the owner on Mar 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
75 lines (75 loc) · 2.29 KB
/
game.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
//input
const playerName = document.querySelector('#player_name');
const playerGuess = document.querySelector('#player_guess');
//buttons
const startGame = document.querySelector('#start_game');
const checkValue = document.querySelector('#check_value');
const ok = document.querySelector('#ok')
//screen
const message = document.querySelector('#message');
//vars
let usrName ='Player';
let random;
let streak=0;
let guess;
//function loading
function load (){
ok.innerText = 'Okay';
ok.style.display = 'none';
playerName.style.display = 'block';
playerName.value = '';
playerGuess.value ='';
playerGuess.innerText='';
startGame.style.display = 'block';
message.innerText = 'Welcome to the guessing game!';
streak=0;
random = Math.floor(Math.random()*100)+1;
}
//function of starting
function read (){
usrName = playerName.value;
message.innerHTML = `Hello <strong>${usrName}</strong>! My name is Willy.<br>Can you guess my age?<br><i>Hint, it is between 1 and 100.</i>`;
playerName.style.display = 'none';
startGame.style.display = 'none';
playerGuess.style.display = 'block';
checkValue.style.display = 'block';
}
//function compare
function compare(){
playerGuess.style.display = 'none';
checkValue.style.display = 'none';
ok.style.display = 'block';
if(isNaN(+playerGuess.value)){
message.innerHTML = 'Please give me a number!'
} else{
guess = +playerGuess.value;
streak++;
if (guess < random){
message.innerHTML = 'Nope! It is more.'
} else if (guess > random){
message.innerHTML = 'Nope! It is less.'
} else{
message.innerHTML = `Yes, that is right. But only after ${streak} guesses.`
ok.innerText = 'Play again!';
}
}
}
//again
function okay (){
if(guess === random){
load();
}else{
message.innerHTML = `So <strong>${usrName}</strong>! What is your new guess?`;
playerName.style.display = 'none';
startGame.style.display = 'none';
ok.style.display = 'none';
playerGuess.style.display = 'block';
checkValue.style.display = 'block';
}
}
//load
load();
//clicks
startGame.addEventListener('click',read);
ok.addEventListener('click',okay);
checkValue.addEventListener('click',compare);