Curious question: Challenge (Profile lookup)

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 :sunglasses:! Thank you so much for answer me
Link to the challenge:

The return statement tells the function to end immediately and return the value of the expression to the right of the return. So your function is returning either true or false depending on if name is equal to contacts[i].firstName. You don’t want to do this and this code does not solve the challenge so I’m quite sure why you said it did?

1 Like

Ahahahaha yeah that’s it! I totally forgot that I missed a failed test, sorry about that! But you’re right

But why do you think it doesn’t iterate through the other positions in the array object?, because it will always evaluate if the name it’s equal to the firstName, only in the first position.

If I remove the bad return then your code still does not pass all of the tests. I am getting the following fail:

lookUpProfile("Bob", "potato") should return the string No such contact

Are you not getting this error?

yup! I just had to anidate the property condition inside the name, since without the name there’s no contact and with that, no need for looking for a property:

if (name == contacts[i].firstName && contacts[i].hasOwnProperty(prop)) {
       if (contacts[i].hasOwnProperty(prop) ) { 
      return contacts[i][prop] ;
    }  else {
        return "No such property";

Srry for the disorder hahahaha

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