Profile Lookup (Help)

This first part should pass the first three tests, any ideas as to what I have done incorrectly? 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 (var i=0; i < contacts.length; i++) {
if (name == "firstName" && contacts[name].hasOwnProperty(prop) == true) {
    return contacts[name][prop] = value;
}
}
// Only change code above this line
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36.

Challenge: Profile Lookup

Link to the challenge:

name == "firstName"

This checks if the name argument is the literal string “firstName”.


contacts[name]

contacts is an array, so you should access its values with a 0-based index. name is a string, not a number.

1 Like

Hello~!

The very first thing I notice is that your if condition will never run, because the name parameter will not equal the string “firstName”. Were you trying to access the object’s firstName property?

1 Like

Here are some additional errors to address:

This will always be undefined. contacts is an array of objects, not an object.

Again, the bracket notation isn’t quite correct here. Additionally, value has not been declared or initialised and will throw an undefined error.

1 Like

Yes, I was trying to see if the name passed was the firstName property, so I’m just accessing everything wrong?

I was assuming it was because i was accessing it like that but what confuses me is that if I do use a zero based index like contact[0], wouldn’t it only check the first object within the array? For it to check the whole collection would it be like contact[0-3]? Which is probably incorrect

You’ve got your loop index i, which corresponds to the indexes of the contacts array (because you initialize i to 0, and the terminal condition is i < contacts.length). You could access each contact in the array in turn by utilizing that i variable within the loop.

1 Like

If you notice then you can see that ‘contacts’ is an array.
Arrays can only be accessed using numbers. For example: contacts[0] or contacts[1] etc.
You have tried to access the array by doing contacts[name].
This you can do if contacts is an object. But it is not. ‘name’ is a variable. Hence your fucntion will not run.

1 Like