lookUpProfile property undefined

Tell us what’s happening:
My code seems to be returning undefined when searching for a missing property instead of returning "No such property" . Please I need help finding my error.

  **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 (var i = 0; i < contacts.length; i++) {
if (name === contacts[i].firstName) {
  return contacts[i][prop];
   } else if (contacts[i][prop] === false) {
     return "No such property";
     }
  } 
  return "No such contact";
// Only change code above this line
}
console.log(lookUpProfile("Sherlock", "dislikes"));
  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36

Challenge: Profile Lookup

Link to the challenge:

@JeremyLT :point_up_2:

Hello there.

Do you have a question?

If so, please edit your post to include it in the Tell us what’s happening section.

Learning to describe problems is an important part of learning how to code.

Also, the more information you give us, the more likely we are to be able to help.


function lookUpProfile(name, prop) {
  // Only change code below this line
  for (let i = 0; i < contacts.length; i++) {
    // You should be checking if the contact has the
    //  property before trying to return the value
    if (name === contacts[i].firstName) {
      return contacts[i][prop];
    } else if (contacts[i][prop] === false) {
     return "No such property";
    }
  } 
  return "No such contact";
// Only change code above this line
}

Thanks for the edit @jwilkins.oboe

You aren’t checking if the property exists before attempting to return it.

I thought that was what I was trying to do here

But that happens after you attempt to return the property on the line above. If the the condition (name === contacts[i].firstName) is true, then the next line executes and your function returns and immediately stops.

1 Like

This part is correct

But inside that condition you need to check if the property exists.
You should review the lesson on Testing Objects for Properties

Then if both of those conditions are true, you can return the value of that property

If both of those conditions aren’t true, then return “No such property”

1 Like

This is SOLVED already but I’d like to ask; what is the difference in the following; and can the two codes do the same thing?

/*
for (var i = 0; i < contacts.length; i++) {
  if (name === contacts[i].firstName) {
    if (contacts[i].hasOwnProperty(prop)) {
      return contacts[i][prop];
    }
}
}

**AND**

for (var i = 0; i < contacts.length; i++) {
  if (name === contacts[i].firstName && contacts[i].hasOwnProperty(prop)) {
      return contacts[i][prop]
    }
*/

The best way to answer this is to look at an actual function call.

Let’s take a look at this function call.

lookUpProfile("Bob", "potato")

Let’s test it with this code.

// 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 (var i = 0; i < contacts.length; i++) {
    if (name === contacts[i].firstName && contacts[i].hasOwnProperty(prop)) {
      return contacts[i][prop]
    }
    else {
      return "No such property"
    }
  }
  return "No such contact";
  // Only change code above this line

When we test this it will return “No such property” because the if condition is false so the else clause is executed.

However, that is a problem because we need our code to return “No such contact.”
If we look at the contacts array, there is no “Bob”

Now let’s look at the correct answer.

function lookUpProfile(name, prop) {
  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 property";
      }
    }
  }
  return "No such contact";
}
console.log(lookUpProfile("Bob", "potato"));

Since this is false, because “Bob” is not a contact

contacts[i].firstName === name

then we return “No such contact”

We only want the else clause to return “No such property” If prop does not correspond to any valid properties of a contact found to match name then return the string No such property .

Hope that helps!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.