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

131
Views
Making function search by name and making an alert if its error

this is my first time posting here, please help me. i want to make else if function to display an alert "error not found" if I search any data was not country name in array data. Here is my code below:

function searchByName(){
    
    let display = document.getElementById("data-container");
    let textInput = document.getElementById("search").value;
    display.innerHTML = "";
    let regex = new RegExp(textInput, 'ig');
  
    for(let i=0;i<country.length;i++){
      if(regex.test(`${country[i].countryRegion}`)){
        display.innerHTML += `
        <h3>${country[i].countryRegion}</h3>
        <div id="data-negara">
        <div id="konfirmasi">
        <h4>Konfirmasi</h4>
        <p>${country[i].confirmed}</p>
        </div>
        <div id="sembuh">
        <h4>Sembuh</h4>
        <p>${country[i].recovered}</p>
        </div>
        <div id="meninggal">
        <h4>Meninggal</h4>
        <p>${country[i].deaths}</p>
        </div>
        </div>
        `} else if( regex.test == false){
            alert("not found");
            break;
        }
    }
    return false;
}

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

In addition to Zai's answer, I think it would help you to remove this part of the code:

if( regex.test == false)

and leave a pure else branch, without any if in it.

Code sandbox here.

about 4 years ago · Juan Pablo Isaza Report

0

Try using trycatch, that’s the easiest solution. The current code in your method cut it and add it to the try section and manage your error handing in the catch block.

Or add an if statement

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!