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:

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?)

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

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

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