Totally lost on Profile Lookup

So I have a vague idea what I need to do, but I’m still really lost. I know I need to combine loops, dot/bracket notation and recursion. But I can’t work out how to do that.

I also can’t work out how the loop helps to do the look up? Why is a loop being used for this process, what purpose does it serve to make this work?

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

lookUpProfile("Akira", "likes");
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Firefox/102.0

Challenge: Basic JavaScript - Profile Lookup

Link to the challenge:

I remembered to do bracket notation for strings this time.
:sweat_smile:

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

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

Edit: wait it isn’t a string. Oh dear I’m getting mixed up.

Hi Ella

Are you still unsure about why you need a loop for this one?

What data type is contacts?

I think it’s needed because it’ll look though the contacts until it finds a matching firstName value. So the loops exit condition has to be finding a matching firstName. But I’m not sure how to write that in code.

It’s an object. Which means I can do hasOwnProperty and dot/bracket notation to access parts of it.

Sure about that?  

oh. Is it an array then?

If not I have no idea what it is.

If it’s an array it doesn’t have a firstName property, or whatever the value of prop is

I think it’s needed because it’ll look though the contacts until it finds a matching firstName value

Yes, that’s exactly right - you need to look at every contact in turn until you find the right one.

And you’re on the right track for how to write that in code - you’ve realised that firstName isn’t a string.

So, what do you need to change to make this comparison work?

name == contacts["firstName"]

1 Like

I have no idea honestly. I’ve been looking at the array lessons but I’m still stuck how to access the parts of the array I need–I can’t use the numbers because I need everything that is firstName, not one firstName.

How do you access the items of an array?

If you weren’t using a loop, to access the firstName property of the first contact in the array, what would you need to code ?

How I did it in this lesson.

I can’t use the numbers, but think I can do something with the i in the loop.

This isn’t right but I think I should be using the i to access things. But I don’t know why that works, if it does work at all.

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

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

Well, yes, you can use the i.

But, you’ve kind of skipped ahead a bit there.

Back to my original question - if you weren’t using a loop, how would you access the firstName property of the first contact?

Something like,

contacts[1]["firstName"]

Close - you’ve got the right idea.

Remember you said (correctly) that the property name isn’t a string?

You’ve used bracket notation - do you remember the other kind of notation that you can use?

1 Like

Oh yeah I can use dot notation, thanks. I wasn’t sure if I could because I didn’t know what the property of the array was. I knew it wasn’t a string, but wasn’t sure what it is.

I have this now, but I’m still doing a lot of things wrong. I don’t know where to go from here or how to say the things I need to say in code to make this work.

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

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

Remember, contacts is an array.
It doesn’t have properties.

This
contacts[1]["firstName"]
was much closer.

Can you change this
contacts[1]["firstName"]
to use dot notation?

I can’t sorry, I have no idea how to turn that into dot notation without writing it as a property.

Also I read that you can’t access arrays with dot notation so I’m really confused now.