Basic JavaScript: Profile Lookup - help me!

can someone tell me why my code is not working please ? it keep telling that your function doesn’t return the value of the property and idk why !
here is my code :

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];
         } else {
             return "No such property";
         return "No such contact";
// Only change code above this line


Is this in the JS course?

yes the Basic JavaScript: Profile Lookup

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.

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.


1 Like

Remember that function execution ends as soon as a return statement is hit. Because your loop has a return statement for every condition, you only ever check the first value in the contacts array.

the returning thing isn’t the problem . i know that the execution ends when it hits return and i did that on purpose

Ok. How is it behaving differently than you expect. It’s not going to pass the tests as it is, so you must be trying to accomplish something else right now.

look what it says

You are not going to pass the tests if you always return on the first contact. It will not work for any name that is not equal to contacts[0].firstName.

1 Like

but i’m using the for loop in here why it can’t be woorking for any name ? sorry i’m really confused

Your code hits a return on the first name.

1 Like

can you fix my code please ? because i feel so desperate

When will you know that a contact does not exist on contacts?

ixt`f(contacts[i].firstName === name) 

with that condition

But that is only checking one name (starting with the first). You know that there is no such contact after checking one name?

it checks first if the first name of contact exists then it checks its given property if its there or not

Not quite. It checks if the name is the same as the firstName property on one item in the contacts array.

yeah it only checks the first name not the rest of the other properties if the first name exists then it goes and check the the required prop

But if you are looking for “Bob” and contacts is something like

const contacts = [
        firstName = "Mary",
        firstName = "Bob",

Then your code will look at contacts[i].firstName and see that “Mary” is not equal to “Bob”, so it will return “No such contact.”

1 Like

ah i see now ! so maybe i’m gonna have to make another loop for that