Help with Profile Lookup

Hey all, just finished my code and this is what I came up with, but not sure why or if i did this right with the if/else statement on the outside of the other if/else statement. Something is wrong with the last else. Can someone please take a look at this code and tell me whats wrong with it? thanks a bunch

var 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(firstName, prop){
// Only change code below this line
for (i=0; i < contacts.length; i++) {
  for(j=0; j < contacts[i].length; j++){
    if (contacts.firstName === firstName){
      console.log(firstName);
      
      if (prop === contacts.lastName) {
        return contacts.lastName;
      }
      else if (prop === contacts.number) {
        return contacts.number;
      }
      else if (prop === contacts.likes) {
        return contacts.likes;
      }
      else {
        return "No such property";
      }
      }
    }
    else {
      return "No such contact";
    }
  }


// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Akira", "likes");

I think that when you do eg if

(prop === contacts.lastName)

You’re checking whether the contents of that property match ‘prop’ but you should actually be testing whether the property itself matches.

Asking for

contacts[index][prop]

will either return the property, if it exists, or undefined if it doesn’t, which should let you test whether the property exists or not.

Are you sure you need 2 loops?

I finished it and I used only 1 FOR loop. In the loop I check for the name first (first IF statement) and if it exists then I look for the property (the 2nd IF statement) then I return appropriate info. I used (and you should have used) .hasOwnProperty(prop) object method to look for the property. It helps me to use lots of console.log() even every line to check up what’s changed and how :wink:
But I hope you figured it out by now :slight_smile:

hey Richard,

Im not exaclty sure what you mean by contacts[index][prop].

for example, do you mean:

if
(contacts[i][prop] === contacts[i].lastname) {
return contacts[i].lastname;
}

does this code make sense?

I guess in this case it is assuming the “prop” variable is already somehow linked to the contacts, or am i stating it here.

thanks,
Allen

@akimbjj77
I think you should use:
object.hasOwnProperty(prop)
method to find out whether there is such property in object
or eventually array[i].hasOwnProperty(prop)
if the objects are nested in the array
And if you’re looking for lastname property in an array of objects you can do it in this way:
array[i].hasOwnProperty(lastname)