Files
2021-07-01 20:25:07 -04:00

48 lines
1.6 KiB
Plaintext
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!doctype html>
<html>
<head>
    <title>Complete JavaScript Course</title>
</head>
<body>
   <span class="val1"></span> <span>+</span> <span class="val2"></span> = <span>
<input type="text" name="answer"></span><button>Check</button>
<div class="output"></div>
<script>
const app = function () {
const game = {};
const val1 = document.querySelector(".val1");
const val2 = document.querySelector(".val2");
const output = document.querySelector(".output");
const answer = document.querySelector("input");
function init() {
document.querySelector("button").addEventListener("click", checker);
loadQuestion();
}
function ranValue(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function loadQuestion() {
game.val1 = ranValue(1, 100);
game.val2 = ranValue(1, 100);
game.answer = game.val1+game.val2;
val1.textContent = game.val1;
val2.textContent = game.val2;
}
function checker() {
let bg = answer.value == game.answer ? "green" : "red";
output.innerHTML += `<div style="color:${bg}">${game.val1} + ${game.val2} = ${game.answer} (${answer.value})</div>`;
answer.value = "";
loadQuestion();
}
return {
init: init
};
}();
document.addEventListener('DOMContentLoaded', app.init);
</script>
</body>
</html>