That’s almost the command to check for the property. You’re thinking of hasOwnProperty.
Careful with dot notation - it only works for the exact literal property name, but you have a variable holding the property name.
That’s almost the command to check for the property. You’re thinking of hasOwnProperty.
Careful with dot notation - it only works for the exact literal property name, but you have a variable holding the property name.
Ooooh yeahhhh!!! that’s right! but, doesn’t it have to be dot notation since we are using hasOwnProperty?
for (let i = 0; i < contacts.length; i++)
if (contacts[i].firstName === name)
if (contacts[i].hasOwnProperty(prop))
or can we do
for (let i = 0; i < contacts.length; i++)
if (contacts[i].firstName === name)
if (contacts[i].hasOwnProperty[prop])
I struggled through this one as well, so I wont pretend to have a firm grasp on how it all worked out. I think I was 75% there before I needed a lifeline. You’re getting along better than me, just keep in mind it is difficult. I made progress sometimes after a nights sleep…mulling over the solutions in my mind unconsciously for hours.
This part is good.
This part will be a problem
function lookUpProfile(name, prop) {
// Only change code below this line
for (let i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === name) {
if (contacts[i].hasOwnProperty[prop]) {
return contacts[i][prop]
}
}
// Only change code above this line
}
}
Cool, that covers a lot of the logic you need. What are the missing possibilities?
ah wait
function lookUpProfile(name, prop) {
// Only change code below this line
for (let i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === name) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop]
}
}
// Only change code above this line
}
}
// Setup
const contacts = [
{
firstName: "Akira",
lastName: "Laine",
number: "0543236543",
likes: ["Pizza", "Coding", "Brownie Points"],
},
{
firstName: "Harry",
lastName: "Potter",
number: "0994372684",
likes: ["Hogwarts", "Magic", "Hagrid"],
},
{
firstName: "Sherlock",
lastName: "Holmes",
number: "0487345643",
likes: ["Intriguing Cases", "Violin"],
},
{
firstName: "Kristian",
lastName: "Vos",
number: "unknown",
likes: ["JavaScript", "Gaming", "Foxes"],
},
];
function lookUpProfile(name, prop) {
// Only change code below this line
for (let i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === name) {
if (contacts[i].hasOwnProperty(prop))
return contacts[i][prop]
} else {
return 'No such contact' || 'No such property';
}
}
// Only change code above this line
}
lookUpProfile("Akira", "likes");
That gives me
the code you have written what part of the instructions cover?
I’m trying to get my code to say
if (contacts[i].firstName === name) {
if (contacts[i].hasOwnProperty(prop))
return contacts[i][prop]
else return no such contact or no such property
Pick on of those at a time.
Do you want to handle “No such contact” or “No such property” first?
No such contact I guess.
Ahhh I’m SO close!!
function lookUpProfile(name, prop) {
for (let i=0; i<contacts.length; i++) {
if (contacts[i][prop] === undefined) {
return 'No such contact'
}
if (contacts[i].firstName === name) {
if (contacts[i].hasOwnProperty(prop))
return contacts[i][prop];
}
}
// Only change code above this line
}
why doesn this makes you say “No such contact”?
I have no idea ): I’m trying to think super hard.
Delete that. do “No such property” first, it’s easier.
I’m trying to understand how you knew that lol. What did you look for that told you that? I’m trying to think like that, but to no avail.
This is my code now: Since I have a nested if statement, it’s kind of throwing me off. If I make another if line, it doesn’t work.
function lookUpProfile(name, prop) {
for (let i=0; i<contacts.length; i++) {
if (contacts[i][prop] === undefined) {
return 'No such property';
}
if (contacts[i].firstName === name) {
if (contacts[i].hasOwnProperty(prop))
return contacts[i][prop];
}
}
// Only change code above this line
}
Step back and look at the big picture for a second.
You want to find the matching contact by checking the name, right?
And you need to check the matching contact for the property, right?
to check name, you do contact.firstName, no? That’s how I would check what the first name is
Syntax follows from the logic you want to express.
You have good syntax for expressing the logic of finding a matching name. I’m trying to prompt you to describe the logic you need for the property before you start guessing syntax. The logic needs to come before the syntax.