Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!