What is the explanation for the For Loop in the profile Lookup exercise?

because you’re just comparing strings, correct?

What is the role of value here?

var value;
  for (var i = 0; i < contacts.length; i++){
    console.log(contacts[i].firstName === firstName, contacts[i].hasOwnProperty(prop));
    if((contacts[i].firstName === firstName) && (contacts[i].hasOwnProperty(prop))){
      console.log('firstName of object matches firstName argument and object has a property with the same name as the value of prop');
      value = contacts[i][prop]; // assign the value of the object's property with name equal to the value of prop to the value variable
      break; // stop searching because you found a match

so you’re just basically telling the computer to take the objects when you type the word value; is value a reference word? note that value is not defined as a variable anywhere so thats confusing to me.

You could call it booboo if you changed it in both places? just an example. I know that’s not descriptive but I’m just clarifying a concept.

i found that it works if I change the variable everywhere. I think I just have to look this over somemore. will talk again later.

i did test it and it worked! haha!

I think I’m going to digest some of this for now and will get back to you later. I’ve also been talking to erica hernandez (another moderator) so I got the full scoop for now. Thanks for the help.

I may follow up with you tomorrow again. sorry I’ve spent like 4 hours on going through this problem word for word today.

Hello guys,

I’ve just expend all morning working on it. Here is the code working!

for (var x = 0; x < contacts.length; x++){
    if ((contacts[x].firstName === name) && (contacts[x].hasOwnProperty(prop))) {
            return contacts[x][prop];
    } 
    else if ((contacts[x].firstName === name) && (contacts[x].hasOwnProperty(prop) === false)){
            return "No such property";
        }
    }

return "No such contact";

The first if was ok, the problem was with the old “else” after the first if, it was breaking it all, i just add another “else if”.

See ya.

1 Like

Can someone explain to me why following code is wrong ?

  • “Kristian”, “lastName” should return “Vos”
  • “Sherlock”, “likes” should return [“Intriguing Cases”, “Violin”]
  • “Harry”,“likes” should return an array
    Those are the points that are not fulfilled for this task, so it looks like first “if” does something wrong… BUT! If i delete last else if and I put it’s return after the whole “for”, it works. I don’t really get that :confused:
function lookUpProfile(name, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
    if (name == contacts[i].firstName && contacts[i].hasOwnProperty(prop)) {
        return contacts[i][prop];
    } else if (name == contacts[i].firstName && !contacts[i].hasOwnProperty(prop)) {
        return "No such property";
    } else if (name !== contacts[i].firstName) {
        return "No such contact";
    }
}
// Only change code above this line
}
2 Likes

why pass it to the variable value? why not return it directly?

here for example, “No such contact” is passed to var value why is that necessary? why not just return 'No such contacts'

The challenge says

If name does not correspond to any contacts then return "No such contact" .

Why isn’t this incorrect?

if (contacts[i].hasOwnProperty(firstName) === false){
      value = "No such contact";
    }

if I’m not mistaking what the above code does is: It checks if contacts has the property firstName NOT if the name corresponds to any of the contacts

Please I need clarification.

When I tried using return directly, I think it stopped the function from iterating through all the values of i before closing.

function lookUpProfile(name, prop){

var value = ' '; 

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

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

        return contacts[i][prop];

        break;

}

    if(name !== contacts[i]['firstName']){

        return 'No such contacts';

        break;

    }

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

        return 'No such property';

        break;

    }

    

}

return;

}

my code so far.

I’m really trying to understand the logic of every line. But I don’t understand why this won’t work

when can you say that there is no such contact, after checking just the first element, or after checking all of them?

I can say there’s no such contact after checking just the first element because

The function should check if name is an actual contact’s firstName and the given property ( prop ) is a property of that contact.

this is quoted from the challenge, the function is supposed to check if name matches firstName not if name matches any contacts’ names.

what am I missing?

that’s is to check if the current one is the right contact, but

If name does not correspond to any contacts then return "No such contact" .

how can you know if it does not correspond to any contact after checking only the first one?

1 Like