Profile Lookup [Need help]

Profile Lookup [Need help]
0.0 0

#1

Not sure what is wrong with my code, or what I am missing. Any help would be appreciated.


//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 === contacts["firstName"]) {
        if (prop === "lastName") {
            return contacts["lastName"];
        }
        else if (prop === "number") {
            return contacts["number"];
        }
        else if (prop === "likes") {
            return contacts["likes"];
        }
        else {
            return "No such property";
        }
    }
    else {
        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 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36.

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


#2

remember that contacts is the array of objects. your for loop goes over the array and represents each contact as i. so you should use contacts and i with bracket notation inside the for loop.

also you can use obj. hasOwnProperty to check the props.

hope that helps!


#3

You are attempting to access contacts as if it is a normal object which has a property named “firstName”. As @megantaylor has pointed out, contacts is an array with indices of zero through the array’s length minus 1. Each element in contacts is a normal object with property names such as “firstName”, “lastName”, “number”, and “likes”.


#4

Hi guys, thanks for the reply. I was able to solve it without reading your responses. It all makes sense now! That what the loop variable is for. I have a few questions though.

Here is my answer:


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

Question 1: How come the return “No such contact” has to be OUTSIDE the for loop?

Question 2: How come for if contacts.hasOwnProperty(prop) we don’t need to put if contacts.hasOwnProperty(prop) === true ?

Thanks for all your help!


#5

Q1: because you only know if there’s no such contact after the for loop has gone through all the contacts
Q2: you could do contacts.hasOwnProperty(prop) === true, but contacts.hasOwnProperty(prop) returns a boolean (true or false), so if you do

if(contacts.hasOwnProperty(prop)){
}

it evaluates as

if(true){
}

or

if(false){
}

#6

Thanks Megan! Great explanations!