Profile Lookup in code Test

Please why is this code not solving the problem. Please someone should tell me what I am doing wrong. Thanks:
Describe your issue in detail here.

  **function lookUpProfile(name, prop) {

// Only change code below this line
for(var i = 0; i < contacts.length; i++) {
if(contacts[i].firstName === name && contacts[i].hasOwnProperty(prop) === true) {
return contacts[i][prop];
}else {
return “No such property”;
}
}
return “No such contact”;
// Only change code above this line
}**


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

lookUpProfile("Akira", "likes");
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36

Challenge: Profile Lookup

Link to the challenge:

Your code only checks contacts[0] because it will either return the property value or it will return the string “No such property”. Remember that a return statement exits the function and no other code in the function is run after that.

2 Likes

Thanks ArielLeslie.

“The function should check if name is an actual contact’s firstName and the given property ( prop ) is a property of that contact.”
considering the above statement which comes with “and” , is the use of “&&” not okay?

The && works correctly as an and. But what happens if contacts[0].firstName does not match name?

2 Likes

I am still new to coding. please try and tolerate my questions.
If contacts[0].firstName does not match name , I think it will return “No such contact”

Nope. You have an if-else. One of those two return statements must execute.

1 Like

Please may you explain the solution for me?

No. We don’t give out answers; that doesn’t benefit either of us! We help you fix your code.

Do you understand how an if-else works? One of the two parts always runs.

if (someCondition === true) {
  // run this code 
} else {
  // otherwise run this code 
}
1 Like

Okay.
Does it mean one can’t have more one condition on if-else statement.
like

if (someCondition === true && condition ===true) {
  // run this code 
} else {
  // otherwise run this code 
}

like what i wrote.

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

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Thanks. I am learning a lot of things from you guys.

You can have more than one condition in an if statement.
If both of the conditions are true, then the code in the if block will run. If either of the conditions are false, the code in the else statement will run.

The problem that I am trying to point out is that it it will always hit a return. That means that only the first value in your array will ever be checked and it will either return a property value or it will return “No such property”. There is no way for this line to ever be reached:

return "No such contact";
2 Likes

Yup. Both the if and the else code use a return, so your function will always return and immediately stop on the first loop iteration.

Thanks guys. I now understand better.

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