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

127
Views
Hacer una función de búsqueda por nombre y hacer una alerta si es un error

esta es la primera vez que escribo aqui, por favor ayúdenme. Quiero hacer otra cosa si la función muestra una alerta de "error no encontrado" si busco algún dato que no sea el nombre del país en los datos de la matriz. Aquí está mi código a continuación:

 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

Además de la respuesta de Zai, creo que te ayudaría eliminar esta parte del código:

 if( regex.test == false)

y dejar una rama else pura, sin ningún if en ella.

Código sandbox aquí .

about 4 years ago · Juan Pablo Isaza Report

0

Intenta usar trycatch, esa es la solución más fácil. El código actual en su método córtelo y agréguelo a la sección de prueba y administre su error al entregar el bloque catch.

O agregue una declaración if

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!