Curious question:
I’ve solved this challenge with the code below, but I was stuck in the beginning since I wasn’t able to iterate through the whole object using a simple for loop until I proved to change the conditions, and even with that I get this curious step where everything just worked and was iterating inside the if statement but not outside, so I overthink the process and I just don’t realize why it happens.
**return name == contacts[i].firstName** ==> Where I just get a false statement
// Object:
const 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 (var i = 0; i < contacts.length; i++){
// Right here I'm asking me why the console prints me out a false when the condition is executed in my conditional
return name == contacts[i].firstName
// Solution code below this line (where you get into the condition)
if (name == contacts[i].firstName && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop] ;
}
if (contacts[i].hasOwnProperty(prop) == false) {
return "No such property";
}
}
// Here I get to the conclusion that if I go through the for loop and the last condition is met into the for... well it'll return the last order inside the code block so that always will be the answer
return "No such contact";
}
console.log(lookUpProfile("Sherlock", "likes"));
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36
Challenge: Profile Lookup
Credits: YOU ! Thank you so much for answer me
Link to the challenge: