Profile Lookup "firstName is not defined"

Hi everyone,
Can anybody tell me why do I keep getting this message:
“firstName is not defined”?
Even if I copy paste the guide’s solution from the forum… :frowning:

//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 === firstName){
      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("Akira", "likes");

Your browser information:

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

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

1 Like

In the above line, you reference a variable named firstName (right side of the ===), but you never defined what firstName is anywhere in your code before this line.

1 Like

Thank you for your answer. But Can you help me understand better?
So I wrote it like this now:

function lookUpProfile(name, prop){
// Only change code below this line
for (var x = 0; x < contacts.length; x++){
if (contacts[x].firstName === “firstName”) {
if (contacts[x].hasOwnProperty(prop)) {
return contacts[x][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”);

But I still don’t get the right outcomes.
Where do I go wrong here?

The change you made does not help you solve the challenge. You are simply checking whether or not the current contact’s firstName property (left side of ===) is equal to the string “firstName”. That is always going to be false, because none of the contacts have a firstName value of “firstName”.

1 Like

Ok, I will keep trying to figure it out. THANK YOU!

Think it should be ‘name’ since that’s the parameter for the function. So:

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

}

3 Likes

Ha! I am so happy now! You are right of course. Thank you so much!

1 Like