Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

159
Visualizações
Having an if statement change a string back and forth

I am trying to make a button change a string from 'Mode One:' to 'Mode Two:', and when pressed again, changed back to 'Mode One:' and so on and so forth. However when I run this, it will change the first time to 'Mode Two:' but when I press it again, it will not go back to Mode One.

Here is my code:

Flip
<script>

    let currentMode = 'Mode one:'

    flip = () => {
        if(currentMode = 'Mode one:'){
            currentMode = 'Mode Two:'
            console.log(currentMode)
        }else if(currentMode = 'Mode Two:'){
            currentMode = 'Mode one:'
            console.log(currentMode)
        }
    }

</script>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

In your if statement, your are assigning a value to a variable. The value of this operation is always true. To compare two values you need the == operator:

<script>

    let currentMode = 'Mode one:'

    flip = () => {
        if(currentMode == 'Mode one:'){
            currentMode = 'Mode Two:'
            console.log(currentMode)
        }else if(currentMode == 'Mode Two:'){
            currentMode = 'Mode one:'
            console.log(currentMode)
        }
    }

</script>

about 4 years ago · Juan Pablo Isaza Relatório

0

Since you are using a single "=", that is not comparing values, it is assigning a value. You want to use "==" or "===":

if(currentMode == 'Mode one:'){
   currentMode = 'Mode Two:'
   console.log(currentMode)
}else if(currentMode == 'Mode Two:'){
   currentMode = 'Mode one:'
   console.log(currentMode)
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda