Return contacts[x][prop] why is x always returning name?

When I run the code below, what gets returned is:

“contacts name” and “property”

however the ‘x’ part of the return statement: return contacts[prop]

seems like it should return any property that x is targeting… but it always just returns the name…?

If I was to tell the computer to return the name then it shoud be: return contacts[name][prop] …??? why is it not this?

  **Your code so far**
// Setup
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) {
// Only change code below this line
for (let x = 0; x < contacts.length; x++)  {
  if (contacts[x].firstName === name)  {
    if (contacts[x].hasOwnProperty(prop))  {
      return contacts[x][prop];
    } else {
      return "No such property";
    }
  }
}
   return "No such contact";
// Only change code above this line
}

lookUpProfile("Akira", "likes");
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36 Edg/102.0.1245.33

Challenge: Profile Lookup

Link to the challenge:

contacts is an array and to reference a specific element in an array, you would use an index (starting at 0 for first element, 1 for second element, etc.). contacts[name] would result in undefined, so that would never work. Each element of the contacts array is an object with various properties (firstName is one of those). If I wanted to directly reference a first name or any other property directly, I would need to know the index of the contacts array ahead of time.

For example, if I wanted to reference Kristian’s number, I would write contacts[3].number.

1 Like

Thank you, that makes total sense. Another thing that is bugging me:

The line: contacts.firstName === name

How does the code know how to identify if a persons name is a name? I would naturally use hasOwnProperty to determine if there is a value in the name index, but that is not in this solution and I don’t understand how ’ name ’ is working there.

‘name’ is just a variable defined in the function to hold any argument at that position. ‘name’, there, represents the first name as defined in the logic, and it could be anything in the property as well.

1 Like

Thank you.

So does that mean that because name was defined as a variable, but wasn’t given a value, then it will return any value from the specified position?

(possible silly question: the position is identified from firstName right?)

Not sure what you are asking here.

name is just a variable with the name name. You could rewrite the function changing name to nameToFind and it will work the same.

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

Thank you! :slight_smile:

Let me rephrase:

How does the code know to always return the firstName of the person? The return statement is returning x, but x is not assigned to name as far as I can tell… so how can the return statement be returning the firstName if all the code says is: return contacts x

The function does not return the firstName of the person (at least not for the challenge tests).

The x is a variable in the for loop. It represents an index of the contacts array.

This function returns two specific strings ("No such property" and "No such contact") when very specific conditions are met. Otherwise, the function is designed to return the value of a property of an object (a contact) in the contacts array if the object’s firstName property matches the value passed to the first argument (name) of the function AND the property (prop) exists in that object.

Ah yes thank you so much !! I get it now. :smiley: :smiley: