Help Me Pls: I can't understand how it works

Help Me Pls: I can't understand how it works
0.0 0

#1

The quest why this code is not working

Blockquote

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


// Change these values to test your function
lookUpProfile("Kristian", "lastName");

Blockquote

but this one works?

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


// Change these values to test your function
lookUpProfile("Kristian", "lastName");

#2

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#3

prop is a variable. When you want to access an object’s property using a variable, you must use bracket notation. You should probably review the following challenges to solidify your understanding of when to use object dot notation or bracket notation.