-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
91 lines (78 loc) · 2.45 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
// TO DO: fix input validation for strings - input should take numbers only, not letters
var randomNumber;
var playerGuess;
var amountOfTries;
var isInputValid;
const guessInput = document.getElementById("guess-input");
const guessButton = document.getElementById("guess-btn");
const guessResult = document.getElementById("guess-result");
const newGameButton = document.getElementById("new-game-btn");
const triesSpan = document.getElementById("tries");
const previousGuessesDiv = document.getElementById("previous-guesses");
const inputLabel = document.getElementById("guess-label");
guessButton.addEventListener("click", function() {
playerGuess = guessInput.value;
validateInput(playerGuess);
if (isInputValid) {
compareNumbers();
updatePreviousGuesses(playerGuess);
updateTries();
guessInput.value = "";
}
});
newGameButton.addEventListener("click", function() {
startGame();
});
function startGame() {
generateNumber();
playerGuess = "";
amountOfTries = 0;
triesSpan.innerHTML = amountOfTries;
previousGuessesDiv.innerHTML = "";
guessResult.innerHTML = "";
guessInput.value = "";
guessButton.classList.remove("inactive");
newGameButton.classList.add("inactive");
}
function generateNumber() {
randomNumber = Math.floor(Math.random() * 101);
console.log(randomNumber);
}
function compareNumbers() {
var player = playerGuess;
var computer = randomNumber;
if (player > computer) {
guessResult.innerHTML = player + " is too high!";
} else if (player < computer) {
guessResult.innerHTML = player + " is too low!";
} else if (player == computer) {
guessResult.innerHTML = "It's " + player + "! You guessed!";
gameOver();
}
}
function updatePreviousGuesses(guess) {
var newPreviousGuess = document.createElement("span");
newPreviousGuess.innerHTML = guess;
previousGuessesDiv.appendChild(newPreviousGuess);
}
function updateTries() {
amountOfTries++;
triesSpan.innerHTML = amountOfTries;
}
function gameOver() {
guessButton.classList.add("inactive");
newGameButton.classList.remove("inactive");
}
function validateInput(inputValue) {
if (isNaN(parseInt(inputValue))) {
inputLabel.innerHTML = "Please input only numbers between 0 and 100";
isInputValid = false;
} else if (inputValue < 0 || inputValue > 100) {
inputLabel.innerHTML = "Please input only numbers between 0 and 100";
isInputValid = false;
} else {
inputLabel.innerHTML = "";
isInputValid = true;
}
}
startGame();