Having problem passing the Javascript Profile Lookup can someone please tell me what's wrong with my code

"Kristian", "lastName" should return "Vos"

"Sherlock", "likes" should return ["Intriguing Cases", "Violin"]

"Harry","likes"  should return an array // tests completed


// 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"]
    }
];

/*console.log(contacts[3].lastName);*/


function lookUpProfile(name, prop){

// Only change code below this line
for(var i=0;i<=(contacts.length-1);i++)
{


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

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



}


// Only change code above this line
}

lookUpProfile("Akira", "likes");

This return statement is inside of your loop over all contacts. What will happen the first time you encounter a name that does not match?

1 Like

I didn’t really get can you please explain a little more why my code is failing with an example if possible because I thought the code inside of else if would not run at all if one of the other if statements are true.

Yes, the code does not run if the other statements are true… but what happen the very first time you encounter a contact that does not match the name?

Let’s use one of the provided examples and some console.log statements:

// 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"]
    }
];

/*console.log(contacts[3].lastName);*/

function lookUpProfile(name, prop){

// Only change code below this line
  for (var i=0; i <= (contacts.length-1); i++) {
    console.log("Checking contact " + contacts[i].firstName);
    if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop)) {
      console.log("Name matches and prop found!");
      return contacts[i][prop];
    } else if (contacts[i].firstName != name) {
      console.log("Name does not match");
      return "No such contact";
    } else if  (contacts[i].hasOwnProperty(prop) == false) {
      console.log("Name matches but prop not found!");
      return "No such property";
    }
  }
// Only change code above this line
}

lookUpProfile("Sherlock", "likes");
1 Like

Thank you. I still don’t understand why the name Sherlock is not matching. I think I should take a break today and look back at it tomorrow. I’m finding it difficult.

I found a solution in youtube but I wanted to understand why this code was not working.

Well, what do you see from the console.log statements I added to your code when you run the above code?

This is my understanding so far. We pass in (“Sherlock”, “likes”), the fist if condition runs and then it should return the value, I don’t understand why is the second else if statement is running when the first one is true.

But your if statement is inside of a loop. Is the first if statement true when you are checking the first contact?

I get the output

Checking contact Akira
Name does not match

This is turning out to be more complicated then I though I will come back to it tomorrow and think thoroughly about what you said. Thank you for taking the time to explain.

1 Like

Sure thing. Taking breaks 100% helps with the learning process.

I will say, you have all of the right code, but your return "No such contact" is in the wrong place in your code to get the effect you want.

1 Like

Thank you so much. I figured what was wrong with my code: For i=0, it enters the first if statement check for name Akira==Sherlock which is false, thus it skips out of the loop onto the next else if statement which turns out to be true Akira!=Sherlock and thus returns “No such contact” immediately exiting the function and stopping all the tests inside of it right? Thus, returning a wrong result.

And when I comment the rest of the else if statements out, it goes over the first if statement over and over again until it finds Sherlock==Sherlock at which it does the other check contacts[i].hasOwnProperty(prop) which says that the contact does have a property of lastName as a result it’ll have two true statements and thus it enters inside the if statement returning “Name matches and prop found!”.

So the goal here is to iterate over this first if statement over and over againto check for all values of i before returning anything or going over to the other if tests right?

Hi, I passed with this code. Now thinking whether this could be simplified further. That is not using two for loops. I know we can do that if we put one if statement inside of the other but just wanted to see whether we can simplify this using this same code but not two for loops. Thank you for your help.

function lookUpProfile(name, prop){

  for (var i=0; i <= (contacts.length-1); i++) {
    console.log("Checking contact " + contacts[i].firstName);
    if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop)) {
      console.log("Name matches and prop found!");
      return contacts[i][prop];
    }
    
  }
for (var i=0; i <= (contacts.length-1); i++) {
 if  (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop) == false) {
      console.log("Name matches but prop not found!");
    return "No such property";}
else return "No such contact";
  }

@anon10002461 @Marmiz @JeremyLT

This is just an option, but not sure if it’s correct.

function lookUpProfile(name, prop){
  for (var i=0; i < contacts.length; i++) {
    if (contacts[i].firstName == name){
     if(contacts[i].hasOwnProperty(prop)) {
       return contacts[i][prop]}
     else if (contacts[i].firstName == name){
    return 'No such property'
    }}}
  return 'No such contact'
}

(pd: Ups, there is an error, and the code won’t run more than once, but maybe you can take out something valuable…)


This one passes the exercise, though I couldn’t check it much. Your turn :smile: :smile:

In case more people answer, I’d ask if there is any simple way to measure code-execution time, just because it could be nice to know it sometimes…

1 Like

Thanks for the reply.

Here is what I finally settled with after 2+ days [ pheww…almost went crazy while trying to understand recursion and this lesson. Even with some slight experiences with Java and C before, I am still finding the Javascript lessons tough unlike HTML and CSS and was starting to doubt whether I myself could make it through this certification and the upcoming ones which are all based on Javascript I assume. I wonder how difficult it would be for other people with no experience with programming (for example my friends who I am helping right now with HTML and CSS final projects)

Anyways, here is the code

function lookUpProfile(name, prop){
  for (var i=0; i <= (contacts.length-1); i++) 
  {
    console.log("Checking contact " + contacts[i].firstName);
    if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop)) {
      console.log("Name matches and prop found!");
      return contacts[i][prop];
    }
    if (contacts[i].firstName == name && !contacts[i].hasOwnProperty(prop))
    {
      console.log("Property does not match");
      return "No such property";  
  }    
  }
  return "No such contact";
}
lookUpProfile("Akira", "address");

That’s a huge improvement in length at least, well done! (I don’t like console logs but they are very useful, I get it ha)

1 Like

The console logs was just to see the work flow that @JeremyLT suggested above which was very helpful in understanding where my code went wrong.