Profile lookup solutions

I feel like I missed something. The solutions provided for the Profile Lookup challenge use .hasOwnProperty() and the “in” operator.

Did we learn either of these? I honestly don’t remember seeing either one in a lesson. Can anyone refer me to the FCC chapter that covered these?

I’m trying to create a solution based on what I’ve learned, and it’s not working but I’m not sure why. Can anyone take a look?

function lookUpProfile(name, prop){
// Only change code below this line
 for (i = 0; i < contacts.length; i++) {
     if (contacts[i].firstName === name) {
        if (contacts[i][prop] === prop) {
            return contacts[i][prop];
        }
        else {
            return "No such property";
        }
        }
        else {
            return "No such contact";
        }
    }
// Only change code above this line
}

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/testing-objects-for-properties

Not sure about in, it’s def covered but maybe later on. The hints contain a few possible solutions, sometimes they may use parts of the language you haven’t been taught yet

What happens if the first contact in the list doesn’t have a matching name?

Thanks for the quick response. If the first contact in the list doesn’t have a matching name, it should continue going through the for loop… right? And then I thought that if it doesn’t come across a matching name, it should refer to: else {return “No such contact”}.

everything inside the loop is executed once each iteration, and only after last line it goes to next iteration

consider this, and think again at what would happen

Recall that a return statement ends the function execution.

Thanks for your help, everyone. I’m starting to understand this but am still hitting a mental roadblock.

In the past, I feel like I’ve used if/else statements in a for loop, but in this case the “else” doesn’t work - we have to put “No such contact” outside of the loop entirely. Is that only because the else in this case would involve a return (that ends the execution)? If I put in, say, console.log or print, would it continue to loop?

a console.log() just prints a value to the console
a return statement gives an output to the function and the function stops

That’s correct. A return statement exits the function completely.