Profile Lookup_2

Profile Lookup_2
0

#1

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


#2

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


#3

learn about dot notation and brackets notation …


#4

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.


#5

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”;}


#6

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?


#7

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

#8

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


#9

Can you be more specific?


#10

Can you be more specific?


#11

This assumes there is an actual property named “prop” in the contacts[i] object. There is no property named “prop” in any of the objects in contacts, so that will always return undefined.

Actually, the following if statement condition will always evaluate to false. Why? Because you need to pay attention to the case used of the property names.

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

#12

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


#13

Do you have to assign what?


#14

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;

#15

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.


#16

prop is a variable and you can only reference an object’s property as a variable one way. This is explained in the challenges you have already passed, but you should definitely review.

I will throw in a 3rd previous lesson in addition to the ones @ArielLeslie already mentioned in her reply, which will hopefully make it even clearer.

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-variables/


#17

I had already responded before seeing this post. No need to assign contacts[i][prop] to another variable. Instead, just return contacts[i][prop].

Plus, if you really wanted to do it the way you wrote with the extra variable, you should have written it:

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

because just like before contacts[i].myProp would be undefined, because there is not an actual property named “myProp” in any of the objects.

I still think you need to review those 3 previous lessons to get it straight when and when you can not use dot notation and how to properly use bracket notation.


#18

Thanks! The

return contacts[i][prop];

worked! I’ll remember that. Thanks again!


#19

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?


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

I missed that, sorry