Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

203
Vistas
Javascript contact list with add and search of person name and their number

I want to ask a question , here I have made a little project of javascript of a contact list where I can add a person with his telephone number , or search for him ,I did everything alright except for a little problem , when I search for an added contact list and make alert of the result , the alert result becomes duplicated , the alert result for each person appears twice , so I want to know how to solve this problem here in javascript , and thanks in advance Here is the code source for it :

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 Respuestas
Responde la pregunta

0

You are looping over item object inside filter function which has two keys, and thus the alert is showing twice. Instead, you should loop over filter results:

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda