-1

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>
Beshoy Hanna
  • 611
  • 2
  • 9
  • 29
Jake t
  • 141
  • 2
  • 13

3 Answers3

0

Two things:

  1. The value of an input and the innerHTML of an element are always strings

  2. = is for assignment, not comparison

So your

if (txt=n1+n2)

will

  1. Assign the result of tacking the n2 string onto the end of the n1 string to txt

  2. Go into the body of the if if the resulting string isn't completely blank

To do addition, you need to convert to number. I go into some detail about how to do that in this answer. To do comparison, use == or ===. It doesn't matter which you use if the operands are of the same type. If they aren't, == will try to coerce them for the purposes of comparison (sometimes in surprising ways), === will not (it'll say they're not equal if they're of different types).

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

The innerHtml would retrieve a string not a number, you'll have to parse it.

In addition, the if(txt=n1+n2) won't check if the first nuber equals the second, it will add the first number to the second, and the result will always be true unliss if the result of addition where false or null or undefined. To check if the first one equals the second one, you'll need to use ==.

function check() {
    var txt = document.getElementById("textarea").value;
    var n1 = document.getElementById("num1").innerHTML;
    var n2 = document.getElementById("num2").innerHTML;
    n1=parseInt(n1);
    n2=parseInt(n2);
    if(txt==n1+n2){hideshow(document.getElementById('correct'));}
    else{hideshow(document.getElementById('incorrect'));}
}
Abozanona
  • 2,261
  • 1
  • 24
  • 60
0
function check() {
var txt = parseFloat(document.getElementById("textarea").value);
var n1 = parseInt(document.getElementById("num1").innerHTML);
var n2 = parseInt(document.getElementById("num2").innerHTML);
alert(txt==n1+n2);
if(txt==n1+n2){hideshow(document.getElementById('correct'));}
else{hideshow(document.getElementById('incorrect'));}
}

You were missing ==, not =. And txt, n1, n2 were strings not numbers. so you had to parseFloat them

Aschab
  • 1,378
  • 2
  • 14
  • 31