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

128
Vistas
Else statement still running after if statement is true (JavaScript)

var b = ['a', 'e', 'i', 'o', 'u']

var inp = prompt('Enter any alphabet to check if its vowel or not').toLowerCase()

for (i = 0; i < b.length; i++) {

if(b[i]===inp){
    alert('The entered value is vowel')
    
}else{
    alert('The entered value is not vowel')
}      

}

I have tried using break after else statement but then the loop is not iterating all indexes in array. Thanks

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

0

The for loop runs once for each index of the array and so it will also execute else at least four times. A better construct would be to check whether the array holds the value using .indexOf():

var b = ['a', 'e', 'i', 'o', 'u']

var inp = prompt('Enter any alphabet to check if its vowel or not').toLowerCase()

if(b.indexOf(inp) > -1){
    alert('The entered value is vowel')
    
}else{
    alert('The entered value is not vowel')
}      

Alternatively, array.includes() could be used:

var b = ['a', 'e', 'i', 'o', 'u']

var inp = prompt('Enter any alphabet to check if its vowel or not').toLowerCase()

if(b.includes(inp)){
    alert('The entered value is vowel')
    
}else{
    alert('The entered value is not vowel')
}      

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