-1

I have a function with 2 if statements inside it. There is a variable set it false, and when the function is called, it is supposed to check whether or not the variable is true or false and do a function based on that when the function is false, it tells the variable to become true, and vise versa with if the variable is false. But when I call the function then it will run the opposite if statement. (ie, if the variable is false, then it will do the things that it only should do if the variable is true) I don't understand why this happening. Hope someone can help! I will include a snippet below of the code.

What should happen:
when you click the button its self should turn this color #5aa897, set the variable to true, and the text should change to "On".

What is happening:
when you click the button it turns the button this color #687980, sets the variable to false, and sets the text to "Off". It should only do this if the variable is set to "true", but it is doing it when the text is set to false.


I have no idea why this is happening. I don't know if I set up my function wrong maybe, or my if statement, but I am stuck.

Hope someone can help.

var x = false;

function arOn() {
  if (x == false) {
    document.getElementById('onOff').innerHTML = "On";
    document.getElementById('ar').style.backgroundColor = "#5aa897";
    x = true;
  }
  if (x == true) {
    document.getElementById('onOff').innerHTML = "Off";
    document.getElementById('ar').style.backgroundColor = "#687980";
    x = false;
  }

}
<button id="ar" onclick="arOn()" class="ar">Auto-Run: <span id="onOff">Off</span></button>

7
  • 5
    but x IS set to true. You set it to true when x is false. So when it checks if it is true, it is! Commented Apr 29, 2021 at 20:25
  • 2
    You should add a return so it doesn´t check the true statement after. Commented Apr 29, 2021 at 20:26
  • 2
    @Pauline Better yet, use if-else. Commented Apr 29, 2021 at 20:27
  • @Herohtar DOH! Yes of course, silly me ;D Commented Apr 29, 2021 at 20:29
  • 2
    Why are you trying to do x - 1 when x == true? Commented Apr 29, 2021 at 20:31

2 Answers 2

2

Think about refactoring this so you can detect the true/false dynamically rather than hard coded:

function arOn() {
var elem1 = document.getElementById('onOff') ;
var elem2 = document.getElementById('ar') ;

elem1.innerHTML = elem1.innerHTML == "On" ? "Off" : "On" ;
elem2.style.backgroundColor = elem2.style.backgroundColor == "#5aa897" ? "#687980" : "#5aa897" ;

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

Comments

1

Here you are doing two if statements in a row. When the first one is false, then the second one is obviously true (because it is equivalent to an else statement). But in your case, since at the end of the first if block you set the variable to true, it makes the function enter the second if statement too. Hence the value of the x variable never really changes.

var x = false;

function arOn() {
  if (x === false) {
    document.getElementById('onOff').innerHTML = "On";
    document.getElementById('ar').style.backgroundColor = "#5aa897";
    x = true;
  } else {
    document.getElementById('onOff').innerHTML = "Off";
    document.getElementById('ar').style.backgroundColor = "#687980";
    x = false;
  }

}
<button id="ar" onclick="arOn()" class="ar">Auto-Run: <span id="onOff">Off</span></button>

1 Comment

As pointed out by @John Tyner, you can actually refactor it to not even need a variable to track the state of the button.

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.