Need help about last return statement

Hello , i need your help ,
i don’t understand why this don’t work

function lookUpProfile(name, prop){
// Only change code below this line
for (var i=0 ; i<contacts.length ;i++){
if (contacts[i]['firstName']===name){
    if(contacts[i].hasOwnProperty(prop)){
        return contacts[i][prop];
    }
        return "No such property";
        
}
}  
// Only change code above this line
}

than this work :

function lookUpProfile(name, prop){
// Only change code below this line
for (var i=0 ; i<contacts.length ;i++){
if (contacts[i]['firstName']===name){
    if(contacts[i].hasOwnProperty(prop)){
        return contacts[i][prop];
    }
        
        
}
return "No such contact";
}  
// Only change code above this line
}

can you explain me ? thanks a lot

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.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

1 Like

Look at where the return “No such contact”; is located in each function. The first function (that does not pass the tests) would return “No such contact” if the firstName property of the first object in the contacts array did not match name. You can not know for sure there is not an object containing a matching name until you have iterated through all the objects in the contacts array.

2 Likes

Oh yes now i see ! we have to iterate all of array that’s why the return is out of the for loops ! thanks a lot