Profile Lookup (or) Operator not working

Tell us what’s happening:
I’m doing this challenge and I want to use a || (or) operator instead of writing an -else - statement, for some reason, it doesn’t work, it only works with an -else - statement. Can you tell me what is wrong?
Thank you in advance.

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 (let i = 0; i < contacts.length; i++) {
    if (name === contacts[i].firstName) {
        if (contacts[i].hasOwnProperty(prop)) {
            return contacts[i][prop] || "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; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 YaBrowser/19.6.3.185 Yowser/2.5 Safari/537.36.

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

A return statement is not a logical condition, it is the resulting action.
How would the browser know which thing to return?

1 Like

Thank you for clarifying this. However, look here, it’s the official video from freecodecamp - why does the (or) operator work here?

You are checking if the object has a property before the or operator is evaluated

Plus, the hasOwnProperty is usually best as you would get No such property even if the object has the property but with falsy values (for example, 0, or false)

1 Like