Profile Lookup_2

Tell us what’s happening:
I can’t figure out the logic. I know contacts[i].firstName gives the correct values, but I cannot get values for name or prop within the function when I console.log them. They should produce “Arika” and “likes” When I run the function in Visual Studio Code I always get “No such contact” so I think the mistake is in the first if statement, but I can’t figure it out. I also don’t know if I’m using undefined properly. The values are declared but when there’s no value associated with them I think that generates an undefined, so I think they’re okay. If not, please let me know and why. Thanks!

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 (let i = 0; i < contacts.length; i++) { // The function should check if name is an actual contact's firstName and the given property (prop) is a property of that contact.
        if ((name===contacts[i].firstName) && (prop===contacts[i].firstName||prop===contacts[i].lastName||prop===contacts[i].number||prop===contacts[i].likes)) {
            return contacts[i].firstName; // If both are true, then return the "value" of that property.
        } else if (contacts[i].name == undefined) {
            return "No such contact"; // If name does not correspond to any contacts then return "No such contact"
        } else if (contacts[i].prop ==undefined) {
            return "No such property";
        } // If prop does not correspond to any valid properties of a contact found to match name then return "No such property"
        // Only change code above this line
      
    }
   
}
    // Change these values to test your function
    
    console.log(lookUpProfile("Akira", "likes"));
// Only change code above this line
}

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

Your browser information:

User Agent is: Chrome/68.0.3440.84 .

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup

Hint: review the difference between dot notation and bracket notation.

1 Like

learn about dot notation and brackets notation …

I’m not following your hint. Arrays should be called like this: contacts[i] and the property should be called: contacts[i].firstName. The “likes” property has a space in the first object, but when I replace my code with contacts[i][likes] it threw an error: likes not defined. In fact, I changed every object to contacts[i][property] and they all threw similar errors. How else would you write this? I went back and reviewed, but I didn’t see anything except a call from one parent array to an array within an object, and there are none like that in this problem. Thanks.

ok now here is some more hint i think now you solve
var myNumber ="";
var checkName="";
for (var i=0;i<contacts.length;i++){
if (contacts[i].firstname===name){ ///try this
checkName=true;
myNamber = i;}
}
//Pseudo code

if checkName is not true {
return "no such contact ";}
if checkName is true {
var property = hasOwnProperty ;} //check it
if checkName is true and property is true {
return contact’s property;}
else if (property is not true){
return “no such property”;}

Thanks for the info. I’ll try your hint in a minute. Would you be willing to tell me what’s wrong with my original code?

It’s still not working. I went to YouTube and found a solution, but although it worked for the presenter, it didn’t work for me. I keep getting “No such contact.” Here’s the 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
    for (let i = 0; i < contacts.length; i++) { // The function should check if name is an actual contact's firstName and the given property (prop) is a property of that contact.
        if (contacts[i].firstname === name) {
            if (contacts[i].hasOwnProperty(prop)) { // If both are true, then return the "value" of that property.
                return contacts[i].prop;
            } else {
                return "No such property";
            }
        }
    }
    return "No such contact";
}
    // Change these values to test your function

    console.log(lookUpProfile("Akira", "likes"));

I suggest reviewing Accessing Object Properties with Dot Notation and Accessing Object Properties with Bracket Notation.

Can you be more specific?

Can you be more specific?

Is that also the case with: contacts[i].hasOwnProperty(prop)? Do I have to assign

I changed the typo and the function returned undefined as you suspected. As far as this code

return contacts[i].prop; 

is concerned, I’m not sure I understand how to fix this. Can you do something like this:

contacts[i].prop === prop;

I wasn’t finished with my reply. Sorry. Do I have to assign prop to another variable like so:

var myProp = contacts[i][prop];
return contacts[i].myProp;

I’m not sure that’s correct.

Thanks! The

return contacts[i][prop];

worked! I’ll remember that. Thanks again!

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

// Change these values to test your function
lookUpProfile(“Harry”, “likes”);

Run and test does not work, I dont know why?

if (contacts[i].firstName === name){

I missed that, sorry

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

If you need some help with this you may want to open a thread just for you using the Ask for help button

It would be easier to help you that way

Do you really need the second loop? Think of what is happening in your code - the first loop is checking if there is an object with a certain name, and if there is returning something. But if there is not an object with that name the loop ends - and now do you really need he second loop?

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums