Basic JavaScript - Profile Lookup

Tell us what’s happening:

Describe your issue in detail here.

Hey. Could you please assist? My code doesn’t pass and I don’t know what is wrong now

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

  }
  
  // Only change code above this line
}

lookUpProfile("Akira", "likes");

Your browser information:

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

Challenge Information:

Basic JavaScript - Profile Lookup

Hello hope you’re fine! Your code looks amazing but i think something matter obviously since it doesn’t pass :sweat_smile:
I’m not pretty sure but i think that the problem in your code lies in the location of your “return” statements. Currently they are placed inside the “for” loop, causing the function to exit prematurely before all contacts are checked.

Hope i was helpful! Happy coding! :+1:

2 Likes

As was mentioned you have an issue with your returns.

I would suggest adding this console statement after the function so you can see what it returns

console.log(lookUpProfile("Kristian", "lastName"))

That should give you a hint as to which return statement you need to fix

hope that helps

3 Likes

Hey sir, thank you for your response it took me a couple of hours to figure it out after seeing your code but at last I figured it out :+1:

1 Like

Hey ma’am I console logged my function and it assisted me alot in pinning down the issue, thanks :+1: