JS Challenge Profile Lookup

I cannot understand why the first IF return contacts[i][prop]; doesn’t work?


// 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.length; i++) {
if(name === contacts[i].firstName && contacts[i].hasOwnProperty(prop)) {
    return contacts[i][prop];
} else if(name !== contacts[i].firstName) {
    return "No such contact";
} else if(!contacts[i].hasOwnProperty(prop)) {
    return "No such property";
}
}
// Only change code above this line
}

lookUpProfile("Akira", "likes");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:81.0) Gecko/20100101 Firefox/81.0.

Challenge: Profile Lookup

Link to the challenge:

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

Your problem is here. You are that if (on the first run through or whatever) that if that name does not equal that particular member’s first name, you want to return "No such contact"; Remember that return doesn’t just exit the for loop, but completely exits the function. So, unless the name asked about is the first one in contacts, then it will bale out of the function - it won’t even check any others. You need to find out of there is a way that instead of exiting the function, that you can just “continue” on to the next iteration of the for loop.

When I change that line, your code passes for me.

1 Like

Hm okay.
I don’t understand this part. Will it be possible to re-write the sentence?

Sure. Bourbon and typing don’t mix. :wink:

You are deciding (on the first run through) that if that name does not equal that particular member’s first name, you want to return "No such contact";

2 Likes

Hello again. I gave a shot. The below is my attempt.
I believe I understood what you pointed out and tried to solve that issue (keeping the function going instead of baling out on the first run). But now i have a new issue. The function doesn’t return “No such contact” when name doesn’t match any in contacts. I thought, after the for loop exhausts itself, “No such contacts” would get executed. Would you please take a look at this if/when you can?


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

You have a typo on this line.

Double check this condition. This won’t confirm that the name is correct.

1 Like

No it won’t. But why is that a reason for not returning “No such contact”?
I thought the steps below when coding. What am I missing?

  1. The first IF condition checks a name is in contacts during iteration.
  2. if it’s not, the for loop ends. (No return executed. Still inside of the function)
  3. return “No such contact”

I show it returning the string.

1 Like

Hello I answered myself while capturing this below. If no name & no property, “No such property” gets executed before "No such contract. I am gonna fix this. don’t know how now, but will solve this.

1 Like

Take a look at that line I pointed out earlier. Where does your code check if the name matches? And where should it check if the name matches? :slight_smile:

1 Like

Thank you. I could not solve it by myself. I had to look at the answer. But what you pointed out and your questions helped me understand better where I was wrong and why the answer was correct.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.