forked from twowheelstogo/TEST-QA-BANRURAL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
117 lines (97 loc) · 3.13 KB
/
index.html
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Juego de adivina tu número</title>
<style>
html {
font-family: sans-serif;
}
body {
width: 50%;
max-width: 800px;
min-width: 480px;
margin: 0 auto;
}
.lastResult {
color: white;
padding: 3px;
}
</style>
</head>
<body>
<h1>Juego Adivina tu número</h1>
<p>Hemos seleccionado un número aleatorío entre 1 a 100. Trata de adivinar el número, en un total de 10 turnos o menos. No te preocupes, te diremos sí el número es más alto o más bajo </p>
<div class="form">
<label for="guessField">Ingresa el número a adivinar: </label><input type="text" id="guessField" class="guessField">
<input type="submit" value="Ingresar el número aleatorio" class="guessSubmit">
</div>
<div class="resultParas">
<p class="guesses"></p>
<p class="lastResult"></p>
<p class="lowOrHi"></p>
</div>
</body>
<script>
let randomNumber = Math.random() * 10;
const ATTEMPS = 5;
const guesses = document.querySelector('.guesses');
const lastResult = document.querySelector('.lastResult');
const lowOrHi = document.querySelector('lowOrHi');
const guessSubmit = document.querySelector('.guessSubmit');
const guessField = document.querySelector('.guessField');
let guessCount = 1;
let resetButton;
function checkGuess() {
let userGuess = guessField.value;
if(guessCount === 1) {
guesses.textContent = 'Número aleatorio anterior: ';
}
guesses.textContent += userGuess + ' ';
if(userGuess === randomNumber) {
lastResult.textContent = '!!!Pérdistes!!!';
lastResult.style.backgroundColor = 'black';
lowOrHi.textContent = '';
setGameOver();
} else if(guessCount === ATTEMPS) {
lastResult.textContent = 'Felicitaciones! adivinaste el número!';
lastResult.style.backgroundColor = 'red';
setGameOver();
} else {
lastResult.textContent = 'Incorrecto! ';
lastResult.style.backgroundColor = 'green';
if(userGuess < randomNumber) {
lowOrHi.textContent = 'El número es mayor!';
} else if(userGuess > randomNumber) {
lowOrHi.textContent = 'El número es menor!';
}
}
guessCount++;
guessField.value = '';
guessField.focus();
}
guessSubmit.addeventListener('click', checkGuess);
function setGameOver() {
guessField.disabled = true;
guessSubmit.disabled = true;
resetButton = document.createElement('button');
resetButton.textContent = 'Comienza un nuevo juego';
document.body.appendChild(resetButton);
resetButton.addeventListener('click', resetGame);
}
function resetGame() {
guessCount = 1;
const resetParas = document.querySelectorAll('.resultParas p');
for(let i = 0; i < resetParas.length; i++) {
resetParas[i].textContent = '';
}
resetButton.parentNode.removeChild(resetButton);
guessField.disabled = false;
guessSubmit.disabled = false;
guessField.value = '';
guessField.focus();
lastResult.style.backgroundColor = 'white';
randomNumber = Math.floor(Math.random()) + 1;
}
</script>
</html>