What's wrong with my code in Profile Lookup Challenge

Hello there.
At the profile lookup challenge in JS Basics, I’ve written the code below but it is not working properly.
Can you tell me where did I get it wrong?

for( let i=0 ; i < contacts.length ; i++ ) {
    if (name === contacts[i].firstName){
      if (contacts[i].hasOwnProperty(prop)){
        return contacts[i].prop;
      } 
      else {
        return 'No such property';
      }
    }
    else {
      return 'No such contact';
    }
  }

Welcome to the forum.

First, please always include a link to the challenge and format the code. Or use the Get Help -> Ask for Help button and it will do both for you.

Below is info on code formatting:


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 (’).

Also please always include all of your code. Maybe not the given data, like the contacts list, but the complete function, just in case.

Anyway, when you return in a function, you leave the function. Period. It is over. The function is done. So, in your code inside the loop:

    if (name === contacts[i].firstName){
      if (contacts[i].hasOwnProperty(prop)){
        return contacts[i].prop;
      } else {
        return 'No such property';
      }
    } else {
      return 'No such contact';
    }

Every single path has a return in it. So, it will never test anything except the first element.

If that is the case, it should still work if it is the first contact. So, I test it with this:

console.log(lookUpProfile("Akira", "number"))

I like to put in log statements if I don’t understand something. So, I do this:

function lookUpProfile(name, prop) {
  console.log('entering function name =', name, ' and prop =', prop)
  for( let i=0 ; i < contacts.length ; i++ ) {
    console.log('\ni =', i)
    console.log('this contact', contacts[i])
    if (name === contacts[i].firstName){
      console.log('name found')
      console.log('has property?', contacts[i].hasOwnProperty(prop))
      if (contacts[i].hasOwnProperty(prop)){
        console.log('property found')
        return contacts[i].prop;
      } else {
        console.log('property not found')
        return 'No such property';
      }
    } else {
      console.log('contact not found')
      return 'No such contact';
    }
  }
}

I see that everything seems to work fine for that first thing in the collection. Why is it returning undefined. Then I notice this line:

return contacts[i].prop;

prop is a variable containing a string, the property we want. Is that how you access a property if the name is in a string?

When I fix that, it can at least look up information for the first contact in the list. Now you’ve got to figure out how to keep it from quitting too early. I was able to get it to work by rearranging a little bit of your logic. You have the right idea, it’s just that something is being done at the wrong time. You’re almost there.

Thanks for the help. I really appreciate it.

BTW, I would also use this for my codes from now on. thanks for the tip too.

First, your explanation was vague but after searching a bit, it totally made sense and now I totally get it. the only problems with my codes were return and the understanding of dot/bracket notation.

Yeah, I mean, I do try to be a little vague - I want you to figure some things out. A big part of being a developer is “figuring things out”. Right now at work, I have to figure out how to do something that none of us have every done. There is no one we can ask. We know it can be done because other apps do it. I just have to do a lot of googling, reading, and “figuring it out”. That’s what we do.

2 Likes