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.
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
}
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.
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?
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
}
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.