Why can't I return string "No such contact" inside the for loop?

profile lookup

function lookUpProfile(name, prop) {

  // Only change code below this line

    for(var n=0 ; n < contacts.length ; n++) {

        if(contacts[n].firstName === name){

            if(contacts[n].hasOwnProperty(prop)) {


                return contacts[n][prop]; 

            } else {

                return "No such property"


        }  else {

            console.log("No such contact");

            return "No such contact"




  // Only change code above this line


Your loop makes only 1 iteration, because you return “no such contact” inside the loop.
So, if the name is not equal to the name of the first object in the given array, your loop stops and you function returns “no such contact”.
The idea is to check every object and to return “no such contact” only if your loop iterated through the whole array.

1 Like

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.