-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.html
135 lines (119 loc) · 4.34 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="Guessing Game">
<meta name="keywords" content="HTML, CSS, JavaScript, Guessing, Game">
<meta name="author" content="Jee Vang, Ph.D.">
<meta name="organization" content="One-Off Coder">
<title>Guessing Game</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.7.2/animate.min.css"
crossorigin="anonymous">
<style>
* {
font-family: monospace;
}
body {
margin: 10px;
}
</style>
</head>
<body>
<div class="jumbotron jumbotron-fluid">
<div class="container">
<h1 class="display-4">Guessing Game</h1>
<p class="lead">
Are you able to guess which numbers the computer will generate?
</p>
<hr class="my-4">
<p>
Try entering the numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
</p>
<p>
<a href="https://github.com/oneoffcoder/lightning-projects/tree/master/html/guessing-game" target="_blank">
Source Code
</a>
</p>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-2">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">Guess</span>
</div>
<input id="guess" type="number" min="1" max="10" type="text" class="form-control">
</div>
</div>
<div class="col-2">
<button type="button" class="btn btn-primary" onclick="checkGuess()">
Submit
</button>
</div>
</div>
<div class="row">
<div class="col-sm">
<div id="answer-div" class="alert alert-primary" style="display: none;" role="alert"></div>
<div id="score-div" class="alert alert-info" style="display: none;" role="alert"></div>
</div>
</div>
</div>
<br />
<br />
<audio id="boing">
<source src="boing.mp3" type="audio/mpeg">
</audio>
<script>
let score = 0;
let total = 0;
let previousIndex = -1;
function getRandomNumber() {
return Math.floor(Math.random() * 10) + 1;
}
function getRandomEffect() {
const effects = ['bounce', 'flash', 'pulse', 'rubberBand', 'shake', 'swing'];
while (true) {
const index = Math.floor(Math.random() * effects.length);
if (index != previousIndex) {
previousIndex = index;
return effects[index];
}
}
}
function checkGuess() {
const guess = document.getElementById('guess').value;
if (guess.length == 0) {
return;
}
const number = parseInt(guess);
const randomNumber = getRandomNumber();
let message = '';
if (number === randomNumber) {
message = `Correct!`;
score += 1;
} else {
message = `Incorrect! Computer generated ${randomNumber}. You guessed ${number}.`;
}
total += 1;
const answerDiv = document.getElementById('answer-div');
answerDiv.innerHTML = message;
answerDiv.style.display = 'block';
const scoreDiv = document.getElementById('score-div');
scoreDiv.innerHTML = `Score = ${score} of ${total}`;
scoreDiv.style.display = 'block';
if (number === randomNumber) {
const effect = getRandomEffect();
playBoing();
scoreDiv.className = '';
scoreDiv.classList.add('alert', 'alert-info', 'animated', effect)
}
}
function playBoing() {
const boing = document.getElementById('boing');
boing.play();
}
</script>
</body>
</html>