Help with Profile Lookup

Help with Profile Lookup
0

#1

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");


#2

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?


#3

You have a couple issues going on in your code. First, you have an extra } on line 47 of your code and then you are missing a } on line 54 of your code to close out the first for loop.

Also, you do not need the 2nd for loop, because no where in your code do you ever reference the variable j in your 2nd for loop.

On each of your if statements, you have something like the following:

if (contacts.firstName === firstName){

contacts is an array of objects. Your 1st for loop should be iterating through each object in contacts. The above if statement attempts to refer to a property called firstName in the array contacts. but instead you should be referring to an object in the array contacts, so the if statement should look like the one below:

if (contacts[i].firstName === firstName){

Making the changes I have mentioned will still not give you a final solution to this problem, but once you make these changes, you should be able to figure out the correct logic for making it work. If you have any more questions, let us know.


#4

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:


#5

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


#6

@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)