I need some help with Profile Lookup

Tell us what’s happening:
I have been trying to get this to work but for some reason it always returns undefined any hints?

Your code so far


// 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
if (name == contacts[3].firstName || contacts[2].firstName || contacts[1].firstName || contacts[0].firstname) {
  if (name == contacts[3].firstName) {
    return(contacts[3].prop)
  }
  if (name == contacts[2].firstName) {
    return(contacts[2].prop)
  }
  if (name == contacts[1].firstName) {
    return(contacts[1].prop)
  }
  if (name == contacts[0].firstName) {
    return(contacts[0].prop)
  }
else {
  return ("No such contact")
}
}
// Only change code above this line
}

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

Your browser information:

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

Challenge: Profile Lookup

Link to the challenge:

This line will cause some bugs for you. The first conditional is correct, but the rest are not. contacts[2].firstName, for example, will evaluate to true (unless undefined). So your if block will execute, but if the nested if statements all evaluate to false, your function will not hit a return statement and will then implicitly return undefined.

also these are always undefined as you can’t use variables with dot notation and the objects don’t have properties named prop

Good catch! I didn’t even notice that :sweat_smile:

ohh thanks i did not think of that

// 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
  if (name == contacts[3].firstName || name == contacts[2].firstName || name == contacts[1].firstName || name == contacts[0].firstname) {
    if (name == contacts[3].firstName) {
      return(contacts[3][prop])
    }
    if (name == contacts[2].firstName) {
      return(contacts[2][prop])
    }
    if (name == contacts[1].firstName) {
      return(contacts[1][prop])
    }
    if (name == contacts[0].firstName) {
      return(contacts[0][prop])
    }
  else {
    return ("No such contact")
  }
  }
// Only change code above this line
}

console.log(lookUpProfile("Sherlock", "likes"))

i almost have it

Two things:

  1. When does your function currently return “No such contact” - and when should it?
  2. What should your function return if the contact exists but the property does not?
  1. It should return that when the contact does not exist but it just keeps returning undefined when i do that
  2. It should return “No such property” but i’m not sure how to make it do that

once the name check, you always return contacts[...][prop]
what happens if the value of prop is not a property of the object?

i’m just gonna watch the video i don’t think im smart enough

You can do this! You’re very much on the right track. :slightly_smiling_face: