Basic JavaScript: Profile Lookup

I am having a hell of a time trying to figure this one out, any help would definitely be appreciated, I’ve been working on this one for a couple days, now.

First, my code:

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

lookUpProfile("Sherlock", "likes");

The point is to search for an object, in an array of objects, using the name in the first parameter of the function, and return the value of the key given in the second parameter.

I can get all six checks for my code, but not at the same time. With the code given, I pass all checks asking for returns of ‘No such contact’ and ‘No such property’, but not the checks asking for a value to be returned.

If I delete my else statement with the ‘No such property’ return,

 else {
          return 'No such property';
        }

I can pass all checks but the one asking for the ‘No such property’ return.

I am so confused. The hints aren’t helping and I don’t want to just look at the answer. Any help is welcome.

Remember, a return statement immediately causes the function to end. Knowing that, how many times is this for loop ever going to execute, regardless of how many items are in objArr?

1 Like

Thank you!

Moved the return outside its own for loop, and bing bang boom it worked. Thank you for the explanation!

1 Like

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