Basic JavaScript - Profile Lookup

Tell us what’s happening:
Describe your issue in detail here.
I have been trying it for hours, but I still can’t figure out why it doesn’t work. I used a for ... of loop for this challenge. Could someone help me with the code below, please?

  **function lookUpProfile(name, prop)  {  
           for (let contact of contacts) {
               if (contact.firstName === name) {
                  return contact[prop] || "No such property"; 
                        } 
                        return "No such contact";  
                      }
                }
           lookUpProfile("Sherlock", "likes");**
// 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 contact of contacts) {
  if (contact.firstName === name) {
    return contact[prop] || "No such property"; 
  } return "No such contact"; 
}
// Only change code above this line
}

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

User Agent is: Chrome/104.0.0.0

Challenge: Basic JavaScript - Profile Lookup

Link to the challenge:

When will this value return?

1 Like

Your for loop works fine. Let’s look at the inside of that loop. Let me format it in a more standard way so it makes the issue more clear:

for (let contact of contacts) {
  if (contact.firstName === name) {
    return contact[prop] || "No such property"; 
  }
  return "No such contact"; 
}

Work through the logic there. If the prop is found, you will return something. If the prop isn’t found you will return “No such contact”. No matter what, on every iteration, you will return something. In other words, you will only get the first iteration of the loop and then you will return something. Is that what you want? I was able to reorder things a little and get it to work.

Also, formatting is important. It makes it easier to read code, which is one of the most important things we do.

1 Like

Yes… I found the problem…

Thank you very much @kevinSmith for the detailed explanation. I put the return at the wrong place then I rewrote it again and again… :exploding_head: :gun: