Reputation: 145
corresponding html:
<html>
<title></title>
<head>
</head>
<body>
<FORM NAME="Calculator">
<TABLE BORDER=4>
<TR>
<TD>
<input type="text" name="Input" Size="22" value="">
<input type="text" name="notepad" value="">
<br>
</TD>
</TR>
<TR>
<TD>
<INPUT TYPE="button" NAME="one" VALUE=" 1 " class ="digit" >
<INPUT TYPE="button" NAME="two" VALUE=" 2 " class ="digit" >
<INPUT TYPE="button" NAME="three" VALUE=" 3 " class ="digit" >
<INPUT TYPE="button" NAME="plus" VALUE=" + " class ="operand">
<br>
<INPUT TYPE="button" NAME="four" VALUE=" 4 " class ="digit">
<INPUT TYPE="button" NAME="five" VALUE=" 5 " class ="digit">
<INPUT TYPE="button" NAME="six" VALUE=" 6 " class ="digit">
<INPUT TYPE="button" NAME="minus" VALUE=" - " class="operand">
<br>
<INPUT TYPE="button" NAME="seven" VALUE=" 7 " class ="digit">
<INPUT TYPE="button" NAME="eight" VALUE=" 8 " class ="digit">
<INPUT TYPE="button" NAME="nine" VALUE=" 9 " class ="digit">
<INPUT TYPE="button" NAME="times" VALUE=" x " class ="operand">
<br>
<INPUT TYPE="button" NAME="clear" VALUE=" c " class ="special">
<INPUT TYPE="button" NAME="zero" VALUE=" 0 " class ="digit">
<INPUT TYPE="button" NAME="Execute" VALUE=" = " class ="solve">
<INPUT TYPE="button" NAME="div" VALUE=" / " class ="operand">
<br>
</TD>
</TR>
</TABLE>
</FORM>
<script type = "text/javascript" src="C:\Users\Quonn\Desktop\QBJS\calculatorjs.js">
</script>
</body>
</html>
javascript:
document.onclick = function(x) {
var info = x.target;
if (info.className === "digit" || "operand")
{
addDigit();
}
else {
math();
}
}
function addDigit() {
alert("x");
}
function math() {
alert("y");
}
x is passed in from button clicks on a calculator. The if statement returns as true even when info.className is something other than digit/operand. What do i need to change in my if statement to make it return false?
Upvotes: 1
Views: 2329
Reputation: 22692
You're not using the ||
operator correctly.
The ||
operator is used to OR two values. It appears between the two values.
From your example:
FIRST it checks the left side of your condition:
info.className === "digit"
If true, the ||
operator returns true (it doesn't evaluate the right side).
Otherwise it evaluates the right side of your condition:
"operand"
This will ALWAYS evaluate to true, because the string "operand" does NOT equal a falsey value.
To fix this, you need to use the correct expression on both sides of the ||
operator:
if (info.className === "digit" || info.className === "operand") {
alert("Yay");
}
Upvotes: 4
Reputation: 10228
a string interpreted as a boolean will always return true.
if (info.className === "digit" || "operand") should be if (info.className == "digit" || info.className == "operand")
Upvotes: 0