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

198
Views
Lista de contactos de Javascript con agregar y buscar el nombre de la persona y su número

Quiero hacer una pregunta, aquí he hecho un pequeño proyecto de javascript de una lista de contactos donde puedo agregar una persona con su número de teléfono, o buscarlo, hice todo bien excepto por un pequeño problema, cuando busco una lista de contactos agregada y una alerta del resultado, el resultado de la alerta se duplica, el resultado de la alerta para cada persona aparece dos veces, así que quiero saber cómo resolver este problema aquí en javascript, y gracias de antemano Aquí está el código fuente para eso :

 alert ("This is a phone book app to add and search for contact list previously added,hope you like it : )"); var contactlist = []; var app = true ; do{ var input = prompt("please tell us if you want to add or search for a contact or exit \n (add/search/exit)"); if (input == "add"){ addcontact(); } if (input == "search"){ searchcontact(); } if (input == "exit"){ app = false; } }while(app) function addcontact(){ var contactins = { name:prompt("please enter the name of contact here"), tel:prompt("please enter the telephone number of the contact here") } contactlist.push(contactins); alert("Contact added successfully"); } function searchcontact(){ var input2 = prompt("do you want to search for the contact through name or telephone number ? \n (name/number)"); if (input2 == "name"){ var inputname = prompt("what name you want to search for ?"); contactlist.filter(function(item){ item.name == inputname ; for(var i in item){ alert("the search result is of " + item.name + " and his telephone number is " + item.tel ); } } ) } if (input2 == "number"){ var inputnumber = prompt("what number you want to search for ?"); contactlist.filter(function(item){ item.tel == inputnumber ; for(var i in item){ alert("the search result is of " + item.name + " and his telephone number is " + item.tel ); } } ) } }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Está recorriendo el objeto item dentro de la función de filtro que tiene dos teclas y, por lo tanto, la alerta se muestra dos veces. En su lugar, debe recorrer los resultados del filtro:

 alert ("This is a phone book app to add and search for contact list previously added,hope you like it : )"); var contactlist = []; var app = true ; do{ var input = prompt("please tell us if you want to add or search for a contact or exit \n (add/search/exit)"); if (input == "add"){ addcontact(); } if (input == "search"){ searchcontact(); } if (input == "exit"){ app = false; } }while(app) function addcontact(){ var contactins = { name:prompt("please enter the name of contact here"), tel:prompt("please enter the telephone number of the contact here") } contactlist.push(contactins); alert("Contact added successfully"); } function searchcontact(){ let results = null var input2 = prompt("do you want to search for the contact through name or telephone number ? \n (name/number)"); if (input2 == "name"){ var inputname = prompt("what name you want to search for ?"); results = contactlist.filter((item) => { return item.name == inputname }) for(var res of results){ alert("the search result is of " + res.name + " and his telephone number is " + res.tel ); } } if (input2 == "number"){ var inputnumber = prompt("what number you want to search for ?"); results = contactlist.filter((item) => { return item.tel == inputnumber; }) for(var res of results){ alert("the search result is of " + res.name + " and his telephone number is " + res.tel ); } } }

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!