I have a math program that randomly generates 2 numbers and displays them in 2 places (eg. num1 and num2) I have created the if statement that will show correct or incorrect depending on user input. I believe the error is in the if else statement
function check() {
var txt = document.getElementById("textarea").value;
var n1 = document.getElementById("num1").innerHTML;
var n2 = document.getElementById("num2").innerHTML;
if(txt=n1+n2){hideshow(document.getElementById('correct'));}
else{hideshow(document.getElementById('incorrect'));}
}
For anyone curious, here is all my code:
function num1() {
var x = Math.floor((Math.random() * 10) + 1);
document.getElementById("num1").innerHTML = x;
}
function num2() {
var x = Math.floor((Math.random() * 10) + 1);
document.getElementById("num2").innerHTML = x;
}
function check() {
var txt = document.getElementById("textarea").value;
var n1 = document.getElementById("num1").innerHTML;
var n2 = document.getElementById("num2").innerHTML;
if(txt=n1+n2){hideshow(document.getElementById('correct'));}
else{hideshow(document.getElementById('incorrect'));}
}
function hideshow(which){
if (!document.getElementById)
return
if (which.style.display=="block")
which.style.display="none"
else
which.style.display="block"
}
p {
display: inline;
vertical-align: top;
}
<link rel="stylesheet" media="all" href="http://hokuco.com/style.css">
<body onload ="num1();num2();hideshow(document.getElementById('correct'));hideshow(document.getElementById('incorrect'));">
<div id ="headline">
<pre><p id="num1" name ="qty"></p> + <p id="num2"name ="qty"></p></pre>
<textarea id ="textarea"></textarea></br>
<button onclick="check();">check</button>
<button onclick="num1();num2();hide(document.getElementById('correct'));hide(document.getElementById('incorrect'));">new</button>
<div id="correct" style="font:24px bold; display: block">Good job!</div>
<div id="incorrect" style="font:24px bold; display: block">Incorrect</div>
</div>