Profile Lookup: Why is this not working

I know it’s messy and long but I want to do it my own noobish way before learning the proper way. Just how I operate. I can’t figure out why this doesnt work.
My code:

// 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) {
 for (let i = 0; i < contacts.length; i++) {

//below is what I'm having trouble with
    if (name == contacts[i].firstName && prop == contacts[i].lastName) {
        return contacts[i].lastName;
    } else if (name == contacts[i].firstName && prop == contacts[i].likes) {
        return contacts[i].likes;
//above is what I'm having trouble with.
    } else if (name !== contacts[i].firstName) {
        return "No such contact";
    } else if (prop !== contacts.[i][prop]) {
        return "No such property";

lookUpProfile("Akira", "likes");

When I console.log contacts[i].lastName or contacts[i].likes I get the proper output. So why doesn’t it accept my answers?

Can you return this statement inside of the loop over all contacts? Remember, a return statement immediately stops your function!


I see what you mean. But now I’ve come across another problem.
If I close the for loop after “return contacts[i].likes;”, “i” becomes undefined because of course, it is defined IN the for loop.

In comes another headache…

Lets step back for a minute.

Your function should return one of three things? What are the three possible outcomes and when do they occur?

I would argue there’s 4 possible outcomes:
return name
return likes(array)
return no contact
return no prop

Not quite. There should be no difference between returning a name and returning the likes. Those are both properties.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.