Skip to content

Commit bb98dc7

Browse files
committed
main-commit
1 parent 4fb5160 commit bb98dc7

15 files changed

+112
-0
lines changed

assets/0.png

5 KB
Loading

assets/1.png

4.11 KB
Loading

assets/2.png

4.05 KB
Loading

assets/3.png

4.03 KB
Loading

assets/4.png

3.92 KB
Loading

assets/5.png

4.05 KB
Loading

assets/6.png

4.18 KB
Loading

assets/7.png

3.63 KB
Loading

assets/8.png

4.24 KB
Loading

assets/9.png

4.11 KB
Loading

assets/plus.png

3.02 KB
Loading

index.html

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<html>
2+
<head>
3+
<style>
4+
.center{
5+
margin: auto;
6+
width: 50%;
7+
padding: 10px;
8+
text-align: center;
9+
}
10+
</style>
11+
</head>
12+
<body>
13+
<h1 id="time"></h1>
14+
15+
<div class="center">
16+
<img src="assets/0.png" id="first-number" width="100"/>
17+
<img src="assets/plus.png" width="100"/>
18+
<img src="assets/0.png" id="second-number" width="100"/>
19+
</div>
20+
21+
<div class="center">
22+
<input type="text" id="guess-text">
23+
<input type="button" value="Submit" id="guess-button">
24+
</div>
25+
<script src="scripts/model.js"></script>
26+
<script src="scripts/controllers.js"></script>
27+
<script src="scripts/view.js"></script>
28+
</body>
29+
</html>

scripts/controllers.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const button = document.getElementById("guess-button");
2+
const number = document.getElementById("guess-text");
3+
button.addEventListener("click", buttonEvent);
4+
5+
6+
function buttonEvent(){
7+
const guess = number.value;
8+
guessNumber(guess);
9+
}

scripts/model.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
let firstNumber = 0;
2+
let secondNumber = 0;
3+
let answer = 0;
4+
roll();
5+
6+
// creates a random number from 0-4
7+
function roll(){
8+
firstNumber = Math.floor(Math.random()*5);
9+
secondNumber = Math.floor(Math.random()*5);
10+
answer = firstNumber + secondNumber;
11+
}
12+
13+
// use Date class to get current time in epoch time
14+
let then = Date.now();
15+
let timeLeft = 5;
16+
let gameOver = false;
17+
let score = 0;
18+
19+
// Callback function for event: Button click
20+
function guessNumber(guess){
21+
if(guess == answer){
22+
roll();
23+
score++;
24+
timeLeft = timeLeft + 5;
25+
}
26+
else{
27+
gameOver = true;
28+
printGameOver("LOSE");
29+
}
30+
}
31+
32+
function main(){
33+
const now = Date.now();
34+
// skip if game has ended
35+
if(gameOver){
36+
return;
37+
}
38+
else if (timeLeft <= 0){
39+
printGameOver("OUT");
40+
}
41+
// recursively prints the digits after a second has passed, 1000ms
42+
else if (now - then > 1000){
43+
timeLeft--;
44+
printFirstDigit(firstNumber);
45+
printSecondDigit(secondNumber);
46+
printTimeRemaining();
47+
then = Date.now();
48+
}
49+
requestAnimationFrame(main);
50+
}
51+
main();

scripts/view.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function printTimeRemaining(){
2+
const timeText = document.getElementById("time");
3+
timeText.innerHTML = `<h2>Time left: ${timeLeft}</h2>`;
4+
}
5+
6+
function printGameOver(status){
7+
if(status === "OUT"){
8+
var message = `<h1>Time's UP!</h1> <p>Your final score was: ${score}</p>`;
9+
}
10+
else{
11+
var message = `<h1>You Lose!</h1> <p>The number was: ${answer}</p> <p2>Your final score was: ${score}</p2>`;
12+
}
13+
document.body.innerHTML = message;
14+
}
15+
16+
// prints state of guess with DOM
17+
function printFirstDigit(number){
18+
document.getElementById("first-number").src = `assets/${number}.png`;
19+
}
20+
21+
function printSecondDigit(number){
22+
document.getElementById("second-number").src = `assets/${number}.png`;
23+
}

0 commit comments

Comments
 (0)