I am struggling with the Profile Lookup challenge

So I am currently struggling with the profile lookup challenge. I find that every challenge I do, I am never successful and end up looking at what other people have done. I know my code below is not completely correct and I am missing certain things out, could anyone please help me to point it out without perhaps giving the answer away so I can at least try on my own first and point out any mistakes I have made. Please and thank you!

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

console.log(lookUpProfile("Akira", "likes"));
1 Like

What is contacts.firstName?

Contacts is the object and firstName is the property within the object that I am targeting. But I don’t think I have written it out correctly

I think you need to reevaluate this idea. I mean, you are technically correct, but not in the sense that you mean.

When in doubt, log it out. Use console.log statement to check that assumption. That should be your first stop when things aren’t matching what you think it should be. Good coders are good debuggers and good debuggers are good detectives.

1 Like

First of all, take a look at the comments I’ve put inside your code.

Refresh your knowledge of how to access arrays with bracket notations, maybe that will help you to utilize the for loop that you do have.

For the property check you might want to look at hasOwnProperty() method.

The last thing, as the if check returns a boolean which means there is no third option so the 3rd and 4th if statement are not needed, but they should rather be under an else statement.

And as kevin said, browser console is a big helper.

1 Like

Hi Kevin, thank you so much for the advice because as soon as I started debugging through the code I quickly figured out the problem and managed to solve it. I realised I was missing out on quite a few things and was not utilising my for loop when contacts.firstName should have been contacts[i].firstName and should have been using the hasOwnProperty for the second if statement. Thanks a lot!

1 Like

Hello! Thank you so much for the comments! Looking at what you wrote made me take a step back and start again. You are right I was not utilising my for loop and when I did everything came together. I placed my code below after working it out for so long!

Thanks again, really appreciate it!

function lookUpProfile(name, prop) {
  let result = 'No such contact';

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

console.log(lookUpProfile("Akira", "likes"));

Congratulations, and keep Learning!
also it will be better if you place [spoiler] tags on your working solution :slight_smile:
needs closing tag similar to HTML except it uses brackets "[ " instead of “<”

I added [spoiler][/spoiler] tags for him.

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