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 pathscript.js
56 lines (56 loc) · 2.56 KB
/
script.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
const nameInterface = document.getElementById('nameInterface');
const guessInterface = document.getElementById('guessInterface');
const declareInterface = document.getElementById('declareInterface');
const declaration = document.getElementById('declaration');
const playersGuess = document.getElementById('playersGuess');
const gameStart = document.getElementById('gameStart');
const playersName = document.getElementById('playersName');
let player;
let random = Math.floor(Math.random()*100) + 1;
let streak = 0;
let gameFinished = false;
function check (){
streak++;//iterating the streak value each time the guess is being checked
let playersInput = playersGuess.value;
if (isNaN(playersInput)||playersInput>100||playersInput<1) {
declaration.innerText = 'Please enter a valid number between 1 and 100.'
} else {
if (playersInput > random) {
declaration.innerText = 'No it is less';
} else if (playersInput < random) {
declaration.innerText = 'No it is more';
} else {
declaration.innerText = `Very nice ${player}! After ${streak} tries you had a correct guess!`;
document.getElementById('tryAgain').innerText = 'New game!';//renaming the button to new game after winning
random = Math.floor(Math.random()*100) + 1;//randomizing a new value for a new game
gameFinished = true;
}
}
nameInterface.style.display = "none";
guessInterface.style.display = "none";
declareInterface.style.display = "block";
}
function again () {
if (gameFinished) { //starting a new game, resetting all values.
nameInterface.style.display = "block";
guessInterface.style.display = "none";
declareInterface.style.display = "none";
playersGuess.value ='';
player = "Player";
document.getElementById('tryAgain').innerText = 'Try again!';
} else { //trying another guess
nameInterface.style.display = "none";
guessInterface.style.display = "block";
declareInterface.style.display = "none";
playersGuess.value =" ";
}
}
gameStart.addEventListener('click', ()=>{
player = playersName.value;
nameInterface.style.display = "none";
declareInterface.style.display = "none";
guessInterface.style.display = "block";
document.getElementById('playersGuessLabel').innerHTML = `Alright <span style = 'color:yellow'>${player}</span>, can you guess my secret number?<br>Hint: it is between 1 and 100.`;
});
document.getElementById('tryAgain').addEventListener('click', again);
document.getElementById('check').addEventListener('click', check);