Basic JavaScript: Profile Lookup problem

I have this code:

//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

var nameNotFound = false;

var propNotFound = false;

for (var i = 0; i < contacts.length; i++) {
   if(name = contacts[i].firstName && contacts[i].hasOwnProperty(prop) === true){
       return contacts[i][prop];
   }
   else if(name != contacts[i].firstName){
       nameNotFound = true;
   }
   else if(contacts[i].hasOwnProperty(prop) !== name) {
      propNotFound = true;
   }
}


if(nameNotFound) { return "No such contact."; }
 
if(propNotFound) { return "No such property."; }

// Only change code above this line
}

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

I get errors:

“Kristian”, “lastName” should return “Vos”

“Sherlock”, “likes” should return [“Intriguing Cases”, “Violin”]

“Bob”, “number” should return “No such contact”

“Bob”, “potato” should return “No such contact”

“Akira”, “address” should return “No such property”

Any ideas what I am doing wrong?

I am noticing a few different problems here. First and foremost, a typo here:
if(name = contacts[i].firstName && contacts[i].hasOwnProperty(prop) === true)

= should be ===
= assigns a value to a variable. So right now this line is setting name equal to the rest of the line. Common mistake I’ve made too many times to count :slight_smile: .

I am still working on a way to help with the rest. I am going over the challenge, and I will try to help you without giving away the answer. I will reply here in a little bit.

My first suggestion is to wrap the last line in a console.log like so:
console.log(lookUpProfile("Akira", "likes"))

This will let you see what is being returned.

You are looping through the contacts correctly. I would get rid of the variables for nameNotFound and propNotFound. They are causing problems.

Consider this:
lookUpProfile("Kristian", "favoriteColor")

Your loop will start at i = 0. It checks if name === contacts[0].firstName. Since contacts[0].firstName is Akira, your else if statement will run, setting nameNotFound to true. Then, when i = 3 and the loop finds Kristian, it will set propNotFound to true.

But your code will return “No such contact”, even though there is such a contact because nameNotFound is checked first.

Another problem is else if(contacts[i].hasOwnProperty(prop) !== name)

Object.hasOwnProperty(prop) returns a Boolean (True or False). Your code is checking if true/false is equal to name (which it will never be).

All of this to say, I would recommend that you reset the code and start from scratch, with a new perspective.

You want to loop through the array, like you did. For each object, check if the name matches (===, not =) the objects firstName. If it does, then check (with another, nested if statement) if it hasOwnProperty(prop). If so, return that prop. If not, return ‘No such property’. After your loop, if no name has been found (no return statement has run), then return ‘No such contact’.

Hope I could help, please let me know if this is confusing or you need further guidance.

Thanks. Now I have this code:

//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

var nameNotFound = false;

var propNotFound = false;

for (var i = 0; i < contacts.length; i++) {
   if((name === contacts[i].firstName || name === contacts[i].lastName) && contacts[i].hasOwnProperty(prop) === true){
       return contacts[i][prop];
   }
 if(name !== contacts[i].firstName || name !== contacts[i].lastName){
       nameNotFound = true;
   }
 else {
     nameNotFound = false;
 }  
if(contacts[i].hasOwnProperty(prop) === false) {
      propNotFound = true;
   }
else {
    propNotFound = false;
}

}


if(nameNotFound) { return "No such contact"; }
 
if(propNotFound) { return "No such property"; }
 
// Only change code above this line
}

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

and I get error: “Akira”, “address” should return “No such property”

But the final test is “Akira”, “address”, not Kristian.

Thanks. I understand why it doesn’t work, but I have no clue how to fix it. Any suggestions?