Basic JavaScript - Profile Lookup (why can't i use bracket notation on...)

Hey, so I’ve been working through this and making I understand every single bit, and the only part i have left is- Why can’t I use bracket notation for the firstName bit in the first if statement? As I recall, bracket notation is allowed to be used for all keys, whereas dot notation is only allowed if the key is a single word.

My logic is, I’m just gonna use bracket notation every single time, and never use dot notation so that it’s one less thing I have to think about. But when I try to run it with bracket notation, it won’t run. but with dot notation, it works.

Apologies if there is a simple and stupid thing I’m doing wrong here, and if I should have researched this more before posting. I just figure one of you will instantly know why- seems like this probably has a simple answer. I appreciate your help!

(just for clarity, .firstName will work, but [firstName] will not.)

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 a = 0; a < contacts.length; a++) {
  if (contacts[a][firstName] === name) {
    if (contacts[a].hasOwnProperty(prop)) {
      return contacts[a][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; rv:109.0) Gecko/20100101 Firefox/117.0

Challenge: Basic JavaScript - Profile Lookup

Link to the challenge:

Explain what this does. What is a? What is firstname?

I think you can use bracket notation but when doing so don’t use [firstName] as this implies you’ve defined firstName as a variable instead you want to use ["firstName"].

2 Likes

ah perfect, thank you!

Luke solved it for me, but if you’re still curious, at the bottom of my post, there is a link to the challenge and the beginning of the instructions explains that part.

I think you misunderstood my initial post :slightly_smiling_face: I was asking those questions to get you to reflect on why your code wasn’t working. I knew why it wasn’t working. I wasn’t asking for myself :slightly_smiling_face:

1 Like

Oh, ok, my bad. And thank you for the help!

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