Look Up Profile

there are four possible firstName properties in the array, so what happens when the current firstName property is different from name but an other one instead matches?

It does not accomplished the condition. It is necessary to get both of them rightly.

but what happens in your code in that situation? - thatā€™s the issue you are having
or one of them anyway

1 Like

and again, prop is the name of the property, contacts[i][prop] is the value of the property, you canā€™t compare them.

1 Like

How can I write that comparation???

once you are sure that you have the right contact, you need to check if it has that property.
You have a few ways:

  • you can use hasOwnProperty method
  • you can use the in operator
  • you can check if contacts[i][prop] is or isnā€™t undefined
1 Like
//Setup
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(name, prop){
// Only change code below this line
for (var i=0; i<contacts.legth; i++){

if (name === contacts[i]["firstName"]){

            if (contacts.hasOwnProperty (prop) ){

            return contacts[prop];

            }


}

}
// Only change code above this line
}

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

I am trying the first case with .hasOwnProperty, but I cannot get any positive result.

contacts is an array, you need to use hasOwnProperty on an object

you were not using just contacts before now, were you?

1 Like
//Setup
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(name, prop){
// Only change code below this line
for (var i=0; i<contacts.legth; i++){

if (name === contacts[i]["firstName"]){

            if (contacts[i].hasOwnProperty (prop) ){

            return contacts[i][prop];

            }


}


}
// Only change code above this line
}

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

I cannot still get the first case correctly.


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

What is happening? The first condition should be accomplished. I do not understand the reason why my code is not functioning rightly.

with this one you should be completing a few of the tests, right?

after that try to do the next step, returning No such property
do you pass also those?

then post your code. And we will see to help you in passing also the No such contact ones

1 Like

that is the problem. I am not getting any result with this code.


//Setup
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(name, prop){
// Only change code below this line
for (var i=0; i<contacts.legth; i++){

if (name === contacts[i]["firstName"]){

            if (contacts[i].hasOwnProperty (prop) ){

            return contacts[i][prop];

            }


}


}
// Only change code above this line
}

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

What should I look for?

with this one you should successfully pass the tests that require the property value

I suggest you put all the function calls from the tests in console.log statements so you see what each one is returning

then try to make the ones that ask for No such property


after that you will need to pass those for No such contact. For this it will be necessary to have all the function calls printing their output, for this I suggest the console.log series I talked above.

If you do it like above, you will see that some calls return No such contact when they shouldnā€™t - so you will need to change something.
Can you think why they return that?


function  lookUpProfile  (  name  , prop  )  {


// Only change code below this line

for  (var  i  = 0  ;  i<contacts.length  ;  i++  ){


if (name  === contacts[i]["firstName"] )  {
  if (  contacts[i].hasOwnProperty  (prop)  )  {


                  return contacts[i][prop];   
         
                  

        }  else  {

                   return "No such property";

          }


}



}  return  "No such contact";


// Only change code above this line


}

// Change these values to test your function


lookUpProfile("Akira", "likes");

console.log(contacts[0]["firstName"])

Thanks! I could do it. It was a long month :scream:. Thanks for your help!

congratulations! you did it!

1 Like