Profile Lookup...something is wrong

Hi,

I wonder what is wrong with my code. Only one part seems to be right: ““Bob”, “number” should return “No such contact””


//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(firstName, prop){
// Only change code below this line
if (firstName === [contacts][firstName] && prop === [contacts][prop]) return [contacts][prop][value];
 else if (firstName !== [contacts][firstName]) return "No such contact";
  else if (prop !== [contacts][prop]) return "No such property";
// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Akira", "likes");

You are dealing with an array of objects. You need to loop through each contact in that array. Using a for-loop is a good move.

1 Like

I think that your problem is that you are trying to access data from your contacts array as an object rather than an array of objects. Try iterating through the array and using your logic to compare the parameters passed in to the current object.

EDIT:
@kevcomedia beat me to it!

2 Likes

First, you need to use hasOwnProperty() to determine if a property exists.
Second, like everyone else stated, you need to iterate over the elements of the array.

Object.prototype.hasOwnProperty()

1 Like

Hi,

I wonder if I am on the right track…it seems to get wrong. It says: "The function should check if firstName is an actual contact’s firstName and the given property (prop) is a property of that contact.

If both are true, then return the “value” of that property." Do I need && sentence?


//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(firstName, prop){
// Only change code below this line
for (i = 0; i< contacts.length; i++) {
if ((contacts.hasOwnProperty(prop)) && firstName === [contacts][firstName])  return [prop][value];
}
 else if (firstName !== [contacts][firstName]) return "No such contact";
// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Akira", "likes");
var target;
var i;
function lookUpProfile(firstName, prop){
    for(i=0; i<contacts.length; i++){
          target = contacts[i];
        if(target.firstName == firstName){
            if(target.hasOwnProperty(prop)){
              return target[prop];
            }else{
                return "No such property";
            }
        }
    }
  return "No such contact";
}
1 Like

Here we loop through each contacts with a for loop.

Then we get each contact and compare their firstname with what the user supplied.

If it exists? proceed, if it doesn’t return “No such contact”.

If it exists, check if the object has that particular property, if yes? then display the value…else return “No such property”.

Just get the idea and implement it your own way.

1 Like

Hi,

One part seems to be right in my code: “Bob”, “number” should return “No such contact” . But it still claims that this is wrong.


//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"]
    }
];

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

// Change these values to test your function
lookUpProfile("Akira", "likes");

Edit: it’s right. I forgot to put r in the word firstName. Thank you for the help for answering.

yes.

.hasOwnProperty evaluates to T or F