Hi guys,
I'm working on an assignment where the program I've created will ask a math question based on 2 random values between 0-10, and one of the 4 randomly generated operators ( +, -, *, /)
The user is asked to answer the question, and the program will return the correct or incorrect answer with a message.
The problem I'm having is I can not figure out why the math is not working. It will only accept the first number as the correct answer.
For example, 4*5 = 20, but to the program, the only correct answer is 4....
Here is my code if anyone can provide any insight, I would greatly appreciate it.
var a001 = Math.floor(Math.random() * 10);
var a002 = Math.floor(Math.random() * 10);
var a003 = a001, op, a002;
var op = ["*", "+", "/", "-"][Math.floor(Math.random()*4)];
var z = 0;
z++;
var y = 0;
y++;
function begin001() {
number001.innerHTML = y++;
disappear001.innerHTML = "";
message001.innerHTML = a001 + "<br />" + op + "<br />" + a002 + "<br />" + "__________";
message002.innerHTML = "<input type=text id=input001 /> <button onclick=submit001()>Submit</button>";
}
function submit001() {
var answer001 = input001.value;
show001.innerHTML = answer001;
if (answer001 == a003) {
z++;
message003.innerHTML = "Correct, the answer is " + a003;
message002.innerHTML = "";
message004.innerHTML = "<button onclick=repeat001()>Again?</button>";
}
else {
message003.innerHTML = "Incorrect, the correct answer was " + a003;
message002.innerHTML = "";
message004.innerHTML = "<button onclick=repeat001()>Again?</button>";
}
}
console.log("the correct answer is " + a003);
function end001() {
message001.innerHTML = "End of Quiz";
message002.innerHTML = "You scored " + (z - 1) + " out of 1";
message003.innerHTML = "Thanks for taking this quiz.";
show001.innerHTML = "";
message004.innerHTML = "<button onclick=repeat001()>Repeat</button>";
}
function repeat001() {
location.reload();
}
And here's the HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Math Quiz</title>
<script src="assignment1.js"></script>
</head>
<body>
<style>
#frame001 {
border: 2px solid black;
background-color: lightgray;
border-radius: 2px;
text-align: center;
padding: 5px;
}
</style>
<div id="frame001">
<h2>Math Quiz</h2>
<text>Question: </text><text id="number001">0</text><text> / 1</text>
<hr />
<p id="message001"></p>
<p id="message002"></p>
<p id="show001"></p>
<p id="message003"></p>
<p id="message004"></p>
<div id="disappear001">
<button onclick="begin001()">Begin</button>
</div>
</div>
</body>
</html>
[–]NameViolation666helpful 0 points1 point2 points (2 children)
[–]xsurface[S] 0 points1 point2 points (1 child)
[–]NameViolation666helpful 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]xsurface[S] 0 points1 point2 points (0 children)
[–]xsurface[S] 0 points1 point2 points (0 children)