0

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.

3
  • currentval = 0 will always be true, since you are assigning instead of comparing. For comparison, use == (does not compare type) or === (includes type). Commented Jul 1, 2019 at 9:53
  • if (currentval = 0) is wrong. if (currentval == 0) is correct. Also var scope in js is limited to its function or object. so you have no access no currentval's value outside your function. e.g: in your last if statement. Commented Jul 1, 2019 at 9:53
  • How could I miss that? Okay, It's one thing to write outside the function code which is on me. But, how can you tell if you should use one equal or two equals? I heard that even experienced developers are having the same problem. Commented Jul 1, 2019 at 14:39

5 Answers 5

3

if (currentval = 0) { it should be if (currentval == 0) { and your if condition is outside of countDown() function.

function countDown() {
  var currentval = document.getElementById("countDownBtn").innerHTML;
  console.log(currentval);
  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>

Sign up to request clarification or add additional context in comments.

Comments

1

The errors you made have already been mentioned, I just wanted to add an answer with a little more modern and concise code. You really shouldn't use inline eventlisteners like onclick, instead use HTMLElement.prototype.addEventListener:

countDownBtn.addEventListener('click', () => {
  if (Number(countDownBtn.textContent)) {
    countDownBtn.textContent -= 1;
  }
  if (!Number(countDownBtn.textContent)) {
    countDownBtn.disabled = true;
    newText.textContent = "You clicked 3 times!";
  }
})
<button id="countDownBtn">3</button>
<p id="newText"></p>

Comments

0

count = 0
function countDown() {
  var currentval = document.getElementById("countDownBtn").innerHTML;
 
  if (count < 3) {
    count = count+1
  }else{
  alert("hi")
  }
}
<button id="countDownBtn" onclick="countDown()">3</button>
<p id="newText"></p>

Comments

0

You have missed "=" operator(currentval == 0)

 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>

Comments

0

Adding to the above answer a more concise code would be:

countDownBtn.addEventListener('click', (e) => {
  if (Number(e.target.innerHTML)) {
    e.target.innerHTML -= 1;
  }else{
    e.target.disabled = true;
    newText.textContent = "You clicked 3 times!";
  }
})
<button id="countDownBtn">3</button>
<p id="newText"></p>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.