0

I have managed to write the code of function for a js array of contacts:

function searchcontacts() {
    var input = document.getElementById("myInput");
    var btn = document.getElementById("myBtn");
    var inputcase = input.value.toUpperCase();
    for (i=0; i < contacts.length; i++){        
        if(contacts[i].name.indexOf(inputcase) != -1) {
            contacts[i].showcontact();
        } 
        else { 
            continue;
            document.write("no result found </br>"); 
        }
    }
}

function showcontact() {
    document.write("Name:"+this.name+"<br>");
    document.write("address:"+this.address+"<br>");
    document.write("email:"+this.email+"<br>");
    document.write("phone number:"+this.phone+"<hr>");
}

function Contact(name,address,email,phone) {
    this.name=name;
    this.address=address;
    this.email=email;
    this.phone=phone;
    this.showcontact=showcontact;
}

and now i'm trying to find a way to return the string "no result found " when nothing is found at all. Because continue; skips totaly this step. Does anyone know how can i do that? New at web developing!Thank you in advance!

5
  • 2
    The quickest would probably be to put continue after document.write... Commented Apr 4, 2018 at 12:44
  • add a counter for results and increment it each time you show a contact. After the loop, if results count is zero, then show no results message Commented Apr 4, 2018 at 12:46
  • A continue is like a goto so having code after it like you do does nothing. Commented Apr 4, 2018 at 12:47
  • i think the @baao is right, you should use continue after document.write Commented Apr 4, 2018 at 12:47
  • @baao: That won't work, because it would write "no result found" for each contact that doesn't match, and you would get a lot of "no result found" outputted along with matching contact records. Also, it's pointless having continue as the last statement in a loop, because it is going to continue the loop anyway at that point Commented Apr 4, 2018 at 12:51

1 Answer 1

2

You need to track how many results you find and check that after the loop, something like this:

function searchcontacts() {
    var input = document.getElementById("myInput");
    var btn = document.getElementById("myBtn");
    var inputcase = input.value.toUpperCase();

    var results = 0;

    for (i=0; i < contacts.length; i++){        
        if(contacts[i].name.indexOf(inputcase) != -1) {
            contacts[i].showcontact();
            results++;
        }
    }

    if(results === 0){
        document.write("no result found </br>"); 
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

yes this worked!!! so easy and something that i had already learned! But i don't have the experience yet to combine the methods! thank you all so much for your time!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.