Totally lost on Profile Lookup

I promise you, you can do this.

You had dot notation here
contacts.firstName

If contacts had been an object, that would have been correct.

contacts[1] is an object.

So, you just need to replace
["firstName"]

with your dot notation for firstName.

It’ll look a bit weird, but don’t worry about that.

I am sorry I still don’t know, I don’t know how I write dot notation for one word. How can I dot notate something that isn’t an object without using a number?

.firstName does not work.

firstName on its own doesn’t

firstName.i does not work.

i.firstName doesn’t work either.

I cannot think of any other ways to try to do this.

You had this:

You just need to replace
["firstName"]
with the dot notation version of firstName i.e. a dot, followed by firstName

When I do that though I get this error though. I don’t know how else to put the dot in front of firstName.

SyntaxError: unknown: Unexpected token (32:12)

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

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

Well, you’ve got the “dot followed by first name”.

But what happened to contacts[1]?

Remember, you had
contacts[1]["firstName"]

You just need to replace
["firstName"]
with the dot notation version of firstName

You need to keep the contacts[1] part.

Don’t put this in your code just yet, just reply - we’re working up to something that you can actually code.

Is it,

contacts[1].firstName

?

If so, why is it correct syntax now for accessing an array?

Yes!

We still have a little more work to do though, so I’m going to ask you to bear with me a while longer.

Just to answer your question first of all:

We know that contacts is an array.

It contains four elements (there are four “things” in the array).
Each of those elements is an object.

So, when we access an element of the array we get an object.
That’s why contacts[1] is an object.

An object has properties that we can access using dot or bracket notation as appropriate.

So,
contacts[1].firstName
gives us the firstName property of the object in contacts[1]

Now, back to where we were (this is all leading up to how/why you can use i in your for loop, by the way).

If I run this console log
console.log(contacts[1].firstName)

I get
Harry

So, which element of contacts is contacts[1] ? (first, second, etc. ?

1 Like

Ooooh!!! Blimey that makes sense now! I didn’t realise it was an array with objects inside. I’ve been working with arrays with objects inside for the past few challenges and never realised. :rofl:

The second because Javascript counts from 0.

1 Like

Note that these two do the exact same thing

contacts[1].firstName
contacts[1]["firstName"]
2 Likes

That’s a very good point - and if I’d realised how hard it would be for Ella, I wouldn’t have gone down that route.
But, we did get there eventually.

1 Like

Yes, Javascript counts from zero.

So, without a loop, to access the firstName property of each of the objects in the array, in order, we’d need to code

contacts[0].firstName
contacts[1].firstName
contacts[2].firstName
contacts[3].firstName

We want to access each element of the array, one at a time.
We’re using a loop to do that.

So we can’t code a number to access the element - the number needs to change each time through the loop.
So, we need “something” that will have the value 0 the first time we go through the loop, the value 1 the second time we go through the loop etc.

Can you tell me what we can use, instead of a number, inside the for loop, to access the correct element of the array?

I’d say,

contacts[i].firstName

Because the i is what we’re using to count though the loop. We start at 0, the first object and go though the length until we reach the last.

that’s correct! now knowing that, you can access the right things inside the loop

1 Like

Well done!

Do you think you’ll be ok to code your if statement now?

1 Like

No. I’m sorry I can’t get it to work still.
I don’t know what to put inside the if to make it do what it’s supposed to.

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].firstName.hasOwnProperty(prop)){
return contacts[i].firstName[prop];
}
}
return "No such contact";
// Only change code above this line
}

This says, in words ‘does the first name of contacts i have the property prop’.

A string is not an object, so it won’t have the property prop

1 Like

What I’m trying to do is say this in code.

The function should check if name is an actual contact’s firstName and the given property (prop ) is a property of that contact.
The function should look through the contacts list for the given firstName parameter.

  • If there is a match found, the function should then look for the given propparameter.
  • If both firstName and the associated prop are found, you should return the value of the prop.

But I can’t work out how. With this bit below I’m trying to say ‘check if ‘name’ is an actual contacts firstName’.

name == contacts[i].firstName

With this bit below I’m trying to say ‘if the given property ‘prop’ is a property of that contact’. I don’t know how else to check for that if I can’t use hasOwnProperty.

contacts[i].firstName.hasOwnProperty(prop)

But I can’t seem to translate this into Javascript.

Right, you want

But you have

What’s the extra thing that’s in my description of your code vs what the challenge is asking for?

The part about prop because I’m trying to find if a string exists?

I don’t know how to test for if a string exists though.

aren’t you trying to check if an object has a property?