Profile Lookup: "firstName is not defined". I can't spot any mistakes regarding the firstName. Please help :)

Profile Lookup: "firstName is not defined". I can't spot any mistakes regarding the firstName. Please help :)
0.0 0

#1

Tell us what’s happening:
“firstName is not defined”. I can’t spot any mistakes regarding the firstName. Please help :slight_smile:

Your code so far

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

js

//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 x = 0; x < contacts.length; x++) {
            if (contacts[x].name === 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");

Your browser information:

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

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


#2

In the line shown above, you have a variable named firstName, but it is not defined (it is not declared anywhere in your code and there is not a function parameter of the same name either).

Maybe you meant to write name (a function parameter) instead of firstName?

Another issue you have in the same line of code is the contacts[x].name reference. None of the objects in contacts have a property named “name”. They do have a property named “firstName” though.


#3

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

^^^ I’ve just noticed my first mistake which was not making firstName a string. The name reference was used to match the above parameter. Would this not work?


#4

Not sure what you mean by “not making firstName a string”. Test out your theory and if you have trouble reply back with your updated code, so we can take a look.


#5

if (contacts[x].name = “firstName”). This is the only thing I’ve changed so far. firstName in the function is now a string to match the first property of each object in the contacts array.


#6

There is no property named “name” in any of the objects, so contacts[x].name refers to undefined.

Also, one equals sign means assignment. For comparisons, you should typically use ===