Confused about my code to return the values of the property

My issue is detailed below.

Good lord, I’m stuck here… I don’t know what to do to return these property values. I think there’s something wrong with my code here. it seems I am at lost and tried a lot and it does not seem to have a fruitful result. I would like to see the answer elsewhere but I am afraid it will make me even dull and does not solve the issue.

I am sorry that this might be a repost, but my mind is kinda overwhelmed.
Please give me a hint or point that the code that might be the culprit

Thanks,

 if (name === contacts[i].firstName && prop === contacts[i]) {
    result = contacts[i][prop];
  **My code below**

// 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[i].firstName && prop === contacts[i]) {
    result = contacts[i][prop];
    return result;
  } else if (name !== contacts[i].firstName) {
    return "No such contact";
  } else if (prop !== contacts[i]) {
    return "No such property";
  }
}
// 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/95.0.4638.69 Safari/537.36

Challenge: Profile Lookup

Link to the challenge:

Prop is suppose to be a property of the object. Contacts[i] is suppose to be the (whole) object. When you get an error, try to console.log each variable and see whether what prints out is as you expected or not, and you can act on it accordingly.

2 Likes

There’s nothing wrong in bein overwhelmed sometimes. It’s the nature of it and you just need to look at it from a different perspective.
A couple of things to note:

  1. You’re testing prop against whole properties, not checking for its presence (prop === contacts[i]). Instead, you can use the .hasOwnProperty() method that we got in the past exercises (checks if there’s a given property and returns a Boolean) with prop as an argument

  2. You’re returning “No such contact” and “No such property” within the loop when the condition for the first if isn’t met. Thus, if you check for any name other than the second, it’ll return “No such contact” and exit the function. Instead, you need to take it out of the loop, so it returns that only if the loop has gone through all of the elements and didn’t find any matches. Same goes for “No such property”, except you need to have an if statement out of the loop that would look like this:

    if (name === contacts[name].firstName && !contacts[name].hasOwnProperty(prop)) {
         console.log("No such property");
    }
    

    Or you can integrate it within the loop.

  3. You didn’t initialize result (You can just return the value without assigning it to a result though)

I’ve rewritten your code to fix these issues in the spoiler below, but I suggest you go ahead and try yourself first after reading these tips.

function lookUpProfile(name, prop) {
  for (let i = 0; i < contacts.length ; i++) {
    if (name === contacts[i].firstName && contacts[i].hasOwnProperty(prop)) {
      let result = contacts[i][prop];
      return result;
    }
    if (name === contacts[i].firstName && !contacts[i].hasOwnProperty(prop)) {
      return "No such property";
    }
    
  }return "No such contact";
}
1 Like

Thanks, kind sir, I’ll try to code it accordingly to your hint and not look at the spoiler. I found the answer but I feel like being betrayed by my own mind, your steps seem to be the ones that I have in mind.
I’ll try it, thanks!