I'm creating a simple task where the text shows up after a user clicks the button 3 times. However, the code does not appear to work as I expected.
I've tried adding double bracket inside the if statement as outlined in the MDN guide without luck and also checked similar questions in Stack Overflow like this one, but they're using jquery, so I have no idea how it works.
Here's my codes:
function countDown() {
var currentval = document.getElementById("countDownBtn").innerHTML;
var newval = currentval - 0;
if (currentval > 0) {
newval = currentval - 1;
}
document.getElementById("countDownBtn").innerHTML = newval;
}
if (currentval = 0) {
document.getElementById("newText").innerHTML = "You clicked 3 times!;
}
<button id="countDownBtn" onclick="countDown()">3</button>
<p id="newText"></p>
I'm sure I'm missing something important here and I will be appreciated for your guidance.
currentval = 0will always be true, since you are assigning instead of comparing. For comparison, use==(does not compare type) or===(includes type).if (currentval = 0)is wrong.if (currentval == 0)is correct. Alsovarscope in js is limited to its function or object. so you have no access nocurrentval's value outside your function. e.g: in your lastifstatement.