Totally lost on Profile Lookup

Here I see prop and contact mentioned:

Here I see prop, contact, and firstName mentioned:

Does that part of the instructions talk about the first name?

1 Like

No. Sorry this lesson is really confusing and complex, I feel like my brain is being tied in knots.

I have this now but it’s still wrong.

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

What is contacts ?

And what is contacts[i] ?

Which of them has properties?

Ultimately, a lot of these challenges with arrays and objects mixed together come down to the question ‘what are you’

  1. What is contacts? (array, object, string, number?)

  2. What is contacts[i]? (array, object, string, number?)

  3. What is 'contacts[i].firstName`? (array, object, string, number?)

  4. What is name? (array, object, string, number?)

  5. What is prop? (array, object, string, number?)

etc

Could I try using typeof to find out which they are?
Then I can work out how they’re what they are.

You could, but they are written there, so you should already know what they are looking at the code

1 Like

You can play around with typeof if you want, but you always need to understand the types of all variables when writing a function. The variable definitions are there, so you don’t really need to use typeof in this case.

1 Like

I know what a string is when I look at it,

‘I am a string’

But I don’t know how to tell that name and prop are strings unless I know what part of the object they refer to. And I don’t because the code doesn’t tell me.

It doesn’t say name = something. I don’t know if that makes sense. Name and prop are supposed to refer to something in the object but it’s hard to understand what they refer to.

It says right here.

Okay that makes sense.

I still don’t know how to solve this. I know what I have to do but I can’t translate it into code. And I don’t know how knowing what they are helps. I don’t know what to do with that information even after finding out with typeof.

Should I put this aside for now and go back to the lessons on loops ect? I’m not sure I’m getting anywhere fast here.

function lookUpProfile(name, prop) {
// Only change code below this line
for (let i = 0; i < contacts.length; i++){
if (name == contacts[i].firstName && prop == contacts[i].firstName) {

return name;
}
}
return "No such contact";
// Only change code above this line
}

Taking a break might be a good idea.

Sometimes, something “clicks” when you come back to a problem after a break.

Your loop looks OK, and you was able to solve that record collection stuff just like yesterday. Remember you were solving that stuff like step by step, nice and slow? I suggest you to solve this one in the same fashion.

As said, consider take a liitle break

1 Like

Doing the typeof helped a lot. I know why these things are strings, objects and arrays now, how to tell which kind they are. And how that affects the notation I need to use.

I’m like 90% there now.

function lookUpProfile(name, prop) {
// Only change code below this line
for (let i = 0; i < contacts.length; i++){
if (name == contacts[i].firstName && contacts[i].hasOwnProperty(prop)) {
console.log(typeof contacts[i]);
return contacts[i][prop];
}

}
return "No such contact";

I think the instructions for this challenge could be written a little clearer. Perhaps part of this challenge is learning how to decipher tangled instructions into code? If you want my simplified instructions then click on the blur below.

Check if the name passed into the function is found in the firstname property of one of the objects in the array.

if the name is found in the firstname property for an object:

  • if that object has the corresponding prop property then return the value of that property
  • else return the string “No such property”.

else return the string “No such contact”

I’ve highlighted some keywords that might help give you a hint as to how to structure your code.

Contacts is the array that holds the objects containing contact information.

Contacts[i] is the objects containing the contact information.

Contacts[i].firstName is a string. It is a property that the objects have.

name is a string. It is the property value of any given property.

prop is a string. It is the property value of any given property in the object.

…to my best understanding anyway.

1 Like

Perhaps that’s the case. As a learner myself, I am sometimes struggling when I build some if… else logic. Exercises like this one are a good practice for me.

“name” is the possible value of a “firstName” property. It says which contact you want infos about.

“prop” is the contact property you want the value of

1 Like

I’m still not sure how to structure my code other than I have done. I don’t know how to interpret this any differently sorry,

And I can’t work out where to put ‘no such property’ I can’t put it inside the loop or outside of it so I’ve got no clue where it goes.

I think it depends upon which section this is in? More explicit algorithmic steps given for the user in the description should be less common as the curriculum goes on, yes?

2 Likes

I bolded those ifs and elses for a reason. Do you think they might translate to actual code? (Hint: yes they do). Take your time and don’t get ahead of yourself. Just focus on one thing at a time. Start with my first if statement and then move on to the if statement embedded in that first if statement. I’m assuming you understand how if/else statements work. The logic for this function is just that, a few if/else statments.

1 Like