Profile Lookup not getting it

Tell us what’s happening:
Hey guys I have long time on this and I am just not getting it, I do not know why my code is not working. Any help help would be really helpful.

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

}
// Only change code above this line
}

lookUpProfile("Akira", "likes");

Your browser information:

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

Challenge: Profile Lookup

Link to the challenge:

When any return statement is executed, the function is immediately exited, even if the for loop has not completed. You are returning a value in the first iteration of the for loop, so there are no further iterations.

You will to rethink your overall algorithm for this challenge. Think about when you can for sure return “No such property” vs. “No such contact”. Make sure to walk through your algorithm with each test case before writing any code.

cool that makes sense let me try something else.

I have added a couple of comments to your code:

function lookUpProfile(name, prop) {
// Only change code below this line
  for (var i = 0; i < contacts.length; i++) {  
    if (contacts[i].firstName.hasOwnProperty(name) && contacts[i].hasOwnProperty(prop)) { 
      return  "value"; // Do you want to return the string "value" here?
    } else if (contacts[i].firstName != name) {
      return "No such contact"; // Have you checked every single name at thsi point?
    } else if (contacts[i] != prop ) {
      return "No such property" ;
    }
  }
// Only change code above this line
}
1 Like

this is is reason why I reurning “value”.
If both are true, then return the “value” of that property.
I think thats the challenge is aking.

I thought I was, I will try something else.

The instructions

If both are true, then return the “value” of that property.

mean that you need to return the value associated with that prop for the name. You don’t want to return the string "value".

For the second comment, this is what @RandellDawson was talking about. Your function stops the very first time it hits a return statement.

OK that I think I am starting to get it now

yes he was right about it, now the hint makes sense.

1 Like

I got all the way up to here, but I am getting a syntax error on the else part

function lookUpProfile(name, prop){
  // Only change code below this line
  for ( var i = 0; i < contacts.length; i++) {  
      if ( contacts[i].firstName.hasOwnProperty(name) {
        if (contacts[i].hasOwnProperty(prop){
          return contacts[i][prop];
        } else { // there is an error on this line that i don't understand
         return "No such property"
        }
      } 
  }
return "No such contact"
  // Only change code above this line
}

Getting close:

function lookUpProfile(name, prop){
// Only change code below this line
  for (var i = 0; i < contacts.length; i++) {  
    if (contacts[i].firstName.hasOwnProperty(name) {
      if (contacts[i].hasOwnProperty(prop) { // Your error is actually up here, check your ()s
        return contacts[i][prop];
      } else {
        return "No such property"
      }
    }
  }
  return "No such contact"
// Only change code above this line
}
1 Like

thats right I forgot the parentheses, I will leave this until here for now and I will get back tomorrow if can not resolve this.

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

You are close. This condition right here is your remaining problem. What should this do?

I am doing what is below
The function should check if name is an actual contact’s firstName and the given property ( prop ) is a property of that contact.

Not quite.

contacts[i].firstName.hasOwnProperty(name)

Broken down, this says, look at contact number i. See if that contact’s firstName property has the property given by the variable name.

1 Like

thank you I changed it to this if ( contacts[i].firstName == name)t and it worked but I dont understand why it din’t work when I use the .hasOwnProperty

object.hasOwnProperty(property) just checks if the object on the left of the dot has a property accessed via the value stored in the property variable.

let myObject = {
  dog: "woof",
  cat: "meow",
}

// Try with a defined property
let checkProp = "dog";
console.log(myObject.hasOwnProperty(checkProp)); // true

// Try with a value
checkProp = "meow";
console.log(myObject.cat.hasOwnProperty(checkProp)); // false
1 Like

Great thanks for all your help man.

1 Like