Profile Lookup...something is wrong

Profile Lookup...something is wrong
0

#1

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");

#2

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.


#3

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!


#4

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()


#5

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");

#6
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";
}

#7

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.


#8

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");

#9

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


#10

yes.

.hasOwnProperty evaluates to T or F