Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda