Profile Lookup question | Javascript

Hi, I can’t understand where I am going wrong with the code. Need help please. I am stuck.

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

Thanks in advance :slight_smile:

HI @deetigupta8 !

There are a few issues here.

The first issue you need to address is the structure of your if/else statement.

Take a close a look at this function call here

lookUpProfile("Kristian", "lastName")

The way you have structured your if/else statement is that if “Kristian” does not exist in this first object here

 {
    firstName: "Akira",
    lastName: "Laine",
    number: "0543236543",
    likes: ["Pizza", "Coding", "Brownie Points"],
  },

then this if statement is false and is never run

    if (name == contacts[i].firstName && contacts[i].hasOwnProperty(prop) == true) {
      return contacts[i].lastName;
    }

Then this else if is also false and never runs

else if (contacts[i].hasOwnProperty(prop) == false) {
      return "No such property."
    }

which means this ends up being true and runs

 else if (name !== contacts[i].firstName) {
      return "No such contact."
    }

The problem is that “Kristian” does actual exist in the contacts array here

  {
    firstName: "Kristian",
    lastName: "Vos",
    number: "unknown",
    likes: ["JavaScript", "Gaming", "Foxes"],
  },

You need to first fix your code so it goes through the entire array and checks for that first name. If that name does exist, then you check if the prop exists.

If we go through the entire array and that name does not exist then you return “No such contact”

Hope that makes sense!

Once you fix that part of the logic, then you have a few small errors here

Spelling error:

Incorrect string here

and incorrect string here

Hope that helps!

Also, I wouldn’t hardcode lastName here

Remember that you want to return the value of that property.
It is best to use the prop parameter

Oh, I thought we are only asked to return lastName if the conditions are true. Fixed the code. Thank you for helping :star2: