This used to be university work now I put it our here for review purposes.
- Could this be constructed better?
- Is there any easier path, performance-wise?
- Could this be optimised?
- Which features and functionalities could/should be added to this game?
<!doctype html>
<html>
<head>
<title>Math.floor</title>
</head>
<body>
<h1> Math.floor: The Maths Game</h1>
<script>
// Universty work
// @author Darik
// Math.floor
var score = 0;
var times = 0;
function questions(e) {
var x = Math.floor(Math.random() * 11);
var y = Math.floor(Math.random() * 11);
var z = 0;
var sign = '';
if (e == 1) {
sign = '+'
z = x + y;
interact(x, sign, y, z)
} else if (e == 2) {
sign = '-'
z = x - y;
interact(x, sign, y, z)
} else if (e == 3) {
sign = '/'
z = x / y;
interact(x, sign, y, z)
} else {
sign = '*'
z = x * y;
interact(x, sign, y, z)
}
}
function interact(x, sign, y, z) {
times++;
var userAnswer = prompt("What is " + x + " " + sign + ' ' + y + "?");
if (z == userAnswer) {
document.write("<p>" + x + " " + sign + ' ' + y + " = " + userAnswer + " is correct </p>");
score = score + 1;
} else {
document.write("<p>" + x + " " + sign + ' ' + y + " = " + userAnswer + " <span> is INCORRECT. The correct answer is </span> " + z + "</p>");
}
}
function _score() {
if (times == score) {
document.write('<p> A perfect score </p>');
document.body.style.backgroundColor = "#FF99CC";
} else {
document.body.style.backgroundColor = "#CCFFFF";
}
document.write('<p> You have answerd ' + score + ' questions out of ' + times + '</p>');
}
questions(2);
questions(1);
questions(4);
questions(3);
_score()
</script>
</body>
</html>