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

130
Visualizações
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 Respostas
Responde à pergunta

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 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