Basic JavaScript - Profile Lookup

Hello, everyone. So far, I have it as if name is the first name, then return the name of that person. I feel that I am on the right track with the loop, but not sure where to go from here (:

Your code so far

// 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.firstName) {
    return prop || 'no such contact';
  }
}
  // Only change code above this line
}

lookUpProfile("Akira", "likes");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Profile Lookup

Link to the challenge:

Can you say in words what this line means?

return property or no such contact

I would also double check this

You have a good idea in words, but I am not sure you understand what you are writing in code
What’s contacts, what’s i, what’s contacts.firstName? what’s prop?

Return what about the property?

Maybe something like this?

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
  console.log(name);
  for (let i = 0; i < contacts.length; i++)
  if (contacts[i].firstName === name) {
    return contacts.firstName[prop];
  }
  // Only change code above this line
}

lookUpProfile("Akira", "likes");

I would ask the same questions still

the value? so [‘value’]?

i is the index of the element it is looping over. contacts.firstName is going to return the value of firstName. and prop is likes, for example.

again, what’s contatcs? does it have a firstName property?
What’s the value of contatcs.firstName?

contacts is the object, which has an array. each section has firstName

so contacts.firstName is the firstName of which section?

Oh, I see what you’re saying. I think you’re implying that when I loop, it will be each section of i, right?

I don’t understand what you mean with each section of i

You’re on the right path to understanding this. @ilenia is helping you break it down. Can you say what your code is returning? That should help with understanding where you need assistance.

// 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
  console.log(name);
  console.log(prop);
  for (let i = 0; i < contacts.length; i++)
  if (contacts.name[i] === name) {
    return contacts.firstName[i].prop;
  }
  // Only change code above this line
}

lookUpProfile("Akira", "likes");

TypeError: Cannot read properties of undefined (reading '0')

I’m just so lost. I don’t know what I should be looking for to put into code. All very abstract. Every time I look at solutions like this for objects and stuff, I ask myself “how did they know that’s what they needed to do?”. What I DO know is the only way to get name is if someone does

contacts.firstName

so to start, I am confident that I would start with this:

for (let i = 0; i < contacts.length; i++)
  if (contacts[i].firstName === name)

That part is a good start.

That looks for a contact with a matching name, right?

How do you get the value of property prop for that contact?

Yes, it looks for a contact with a matching name indeed. Glad I got that right!

To get the value of property prop, I would do this…for example, if I wanted to get the value of likes, I would do

contacts.likes

which is “Pizza”, “Coding”, “Brownie Points”.

if contacts has the property

if (contacts[i].hasPropertyOf(prop))