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

111
Vistas
if else print at the same time in JavaScript

when I trigger the if condition, why the else will print out as well? someone could indicate what's the problem of the code below? written in JS.

original picture of code

const rainbow = ['red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'violet'];
let colour = prompt("What is your favourite colour?").toLowerCase();

for (let i = 0; i < rainbow.length; i+=1) {
  if (rainbow[i] === colour) {
    console.log("yes")
  }
  else {
    console.log(rainbow.join(" *** "));
  }
}

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

It's because your loop is running through the array. So for example if I say red it will go to the first if and then after the second iteration it will go to the else. If this code is un a function you need to return in your if, if you want the loop to stop at this moment.

const findColor = () => {
    const rainbow = ['red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'violet'];
    let colour = prompt("What is your favourite colour?").toLowerCase();

    for (let i = 0; i < rainbow.length; i+=1) {
      if (rainbow[i] === colour) {
        console.log("yes")
        return rainbow[i]// It will leave the loop when it passes on the right color
      }
      else {
        console.log(rainbow.join(" *** "));
        return
      }
    }
}

You can also use the includes function if you don't want to use a loop:

console.log(rainbow.includes(colour) ? 'yes' : rainbow.join(" *** "));
about 4 years ago · Juan Pablo Isaza Denunciar

0

You don't even need a loop for this You can use the include function for arrays to achieve this.

const rainbow = ['red','orange','yellow','green','cyan','blue','violet']; 
let colour = prompt("What is your favourite colour?").toLowerCase();

console.log(rainbow.includes(colour) ? 'yes' : rainbow.join(" *** "));

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could use the includes method for checking the input.

const rainbow = ['red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'violet'];
let colour = prompt("What is your favourite colour?").toLowerCase();

if (rainbow.includes(colour)) {
  console.log("yes")
}
else {
  console.log(rainbow.join(" *** "));
}

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