Reputation: 11
Okay so I'm putting together a test that will be the core of something I'm trying to achieve, still learning javascript so any help would be appreciated.
So I've set up a simple test with three buttons, first button shows where the count is at with a numerical value, the second button increases the count, and the last button is what I'm trying to use to get different results based on where the count is at.
Problem is that the third button only ever sees a count of 1, regardless of it being increased with the second button, and the count is also reset by hitting it.
var count = 1;
function getCount() {document.getElementById("demo").innerHTML = count;}
function incCount() { count++ }
function shoCount()
{
if (count = 1)
{document.getElementById("test").innerHTML = "A";}
else if (count = 2)
{document.getElementById("test").innerHTML = "B";}
else if (count > 2)
{alert('invalid');}
}
<button onclick="getCount()">getCount</button>
<button onclick="incCount()">incCount</button>
<button onclick="shoCount()">shoCount</button>
<p id="demo"></p>
<p id="test"></p>
</body>
Upvotes: 1
Views: 71
Reputation: 2560
In javascript you have to use either ==
or ===
for comparison. First is known as abstract equality it will resolve the data type and compare the values for you. Second is known as strict equality it will first check if the type (ex: string, number, boolean etc) on the left hand side is same as the value being compared on the right hand side.
For example:
console.log(4 == "4"); // true (abstract equality)
console.log(4 === "4"); // false (strict equality)
In your case, you can modify the shoCount function as follows to be safe,
function shoCount() {
if (count == 1) {
document.getElementById("test").innerHTML = "A";
} else if (count == 2) {
document.getElementById("test").innerHTML = "B";
} else if (count > 2) {
alert('invalid');
}
}
Upvotes: 0
Reputation: 131316
The problem is the way you use to compare count
with a number
:
This if (count = 1)
returns true
.
It is an assignment operator
and if (1)
is evaluated to true
in JavaScript
So you enter always in this statement :
{document.getElementById("test").innerHTML = "A";}
What you want is the equality comparator ==
:
if (count == 1)
{document.getElementById("test").innerHTML = "A";}
else if (count == 2)
{document.getElementById("test").innerHTML = "B";}
else if (count > 2)
{alert('invalid');}
}
Upvotes: 3
Reputation: 1291
change your script to use == or ====
if (count === 1)
{document.getElementById("test").innerHTML = "A";}
else if (count === 2)
{document.getElementById("test").innerHTML = "B";}
else if (count > 2)
{alert('invalid');}
}
Upvotes: 1
Reputation: 123
Use equality comparator(== or ===) instead of assignment operator(=) in your if statement
Upvotes: 1
Reputation: 176
Use == or ===
if (count = 1) // 1 will be assigned to count
if (count == 1)
{document.getElementById("test").innerHTML = "A";}
else if (count == 2)
{document.getElementById("test").innerHTML = "B";}
else if (count > 2)
{alert('invalid');}
}
Upvotes: 1