Can anyone fix this?

Tell us what’s happening:
Can anyone explain me what is wrong with my code or logic and fix this?

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 (contacts(i).firstName === name && contacts(i)[firstName] === 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("Akira", "likes");
console.log(lookUpProfile("Akira", "lastName"));

Your browser information:

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

Challenge: Profile Lookup

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

return contacts[i][prop];
    } else {
        return "No such property";
    }
}
return "No such contact";

I think the problem is here. You have the return statement outside of your function

I already tried this and it’s not about this.

If you compare the parenthesis here:
contacts(i).firstName === name && contacts(i)[firstName] === prop

…and here:
return contacts[i][prop]

…I think you’ll solve it.

`
//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 (contacts[i].firstName === name && contacts[i].prop === prop) {
return contacts[i][prop];
} else {
return “No such property”;
}
}
return “No such contact”;

// Only change code above this line
}
console.log(lookUpProfile(“Kristian”, “lastName”));

// Change these values to test your function
lookUpProfile(“Akira”, “likes”);
console.log(contacts[1].lastName);
`

AHH OK i made this… and still not working
if (contacts[i].firstName === name && contacts[i].prop === prop) {
return contacts[i][prop];

contacts(i) implies that “contacts” is a function.

contacts[i] gets the value of position i from the contacts array.

This is correct:
contacts[i].firstName === name
…because you want to check if the name exists in the array.

This is the part you need to look over:
contacts[i].prop === prop

…because none of the instances of the array has a property called ‘prop’, they are called something else. You need to find a way to look up a property (eg. “lastName”) on a given object.

what about the task “… and the given property ( prop ) is a property of that contact.”
console.log(contacts[1].lastName); // Potter

If you look at the third test for instance, you’re first going to check if there is an object in the array where the first name is ‘Harry’. If that is true, you’re then going to look through the properties firstName, lastName, number and likes to see if Harry has either of these in his ‘profile’. So in this case:
contacts[1].likes === prop

…would be true and your return should:
return contacts[1].likes

You just have to figure out how to do tge same for all cases. :slight_smile:

you need to check if the object has that property, if you do contacts[i].prop === prop ypu are checking if the property named prop (which actually doesn’t exist, as no object has a property named prop) has the same value of the variable named prop, prop can have a value like lastName but no property has ever that value. (remember that properties are like name: value) THe properties can have that name, but never that value

1 Like

totally stuck
don’t undestand why this don’t work :

(contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)

and this work:

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