Trouble with Profiel Lookup & curious to learn more

Tell us what’s happening:
Describe your issue in detail here.

Why do I need to put return "No such contact" outside of for loop inside the function?
I am in trouble while putting return "No such contact" inside the else statement. The result doesn’t match if I do so! lookUpProfile("Bob", "number") doesn’t match.
Could you please explain to me? I want to learn more.

Thanks in advance

Akram

  **Your code so far**

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

}
return "No such contact";

// Only change code above this line
}

console.log(lookUpProfile("Akira", "address"));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0

Challenge: Profile Lookup

Link to the challenge:

Your function immediately stops as soon as it hits a return statement.

You can actually get rid of that second else if to have the code look like

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 (let i = 0; i < contacts.length; i++) {
  if (contacts[i].hasOwnProperty(prop) && name === contacts[i]["firstName"]) {
    return contacts[i][prop];;
  } else if (name === contacts[i]["firstName"] && !contacts[i].hasOwnProperty(prop)) {
        return "No such property";
  }
}
return "No such contact";

// Only change code above this line
}

console.log(lookUpProfile("Akira", "address"));

Like Jeremy is saying your function is going to go through the loop and look for the conditions you set. If it finds a matching condition then it return the message you set, and the loop stops because it matched a return.

In the code I posted your function will look through the entire loop, and if it finds that " name does not correspond to any contacts" it returns the “no such contact”

That is why it is on the outside, so you go through the entire loop before returning that message. Hope that makes sense.

1 Like

Thank you for your help.

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