How do I make one if statement run after the other has run and the button is clicked again?

37 Views Asked by At

I've tried making if statements that activate other if statements, but both of them ran at the same time.
I also tried else if statements, but that didn't seem to work...
So, how would I make: an if statement that activates once the past if statement has been run and the button is pressed again.
I hope that makes any sense in y'alls head. The help would be appreciated. :)

let One = true
let Two = false
let Three = false
function right() {
    if (One === true) {
        if (Two === false) {
            document.getElementById("2").innerHTML = 1
            document.getElementById("1").innerHTML = ''
            Two = true
            One = false
        } else {
            if (Three === false) {
                document.getElementById("3").innerHTML = 1
                document.getElementById("2").innerHTML = ''
                Three = true
                Two = false
            }
        }
    }
}
1

There are 1 best solutions below

0
Syed Danish Khawar On BEST ANSWER
 let state = 1;

function right() {
  if (state === 1) {
    document.getElementById("2").innerHTML = 1;
    document.getElementById("1").innerHTML = '';
    state = 2;
  } else if (state === 2) {
    document.getElementById("3").innerHTML = 1;
    document.getElementById("2").innerHTML = '';
    state = 3;
  } else if (state === 3) {
    document.getElementById("1").innerHTML = 1;
    document.getElementById("3").innerHTML = '';
    state = 1;
  }
}

the state variable keeps track of the current state. Each time the right function is called, it checks the value of state and updates the elements accordingly. The state variable is then updated to the next state so that the next if statement will run on the next button click.