Trouble with Profile Lookup in java

I’m struggling here. As far as I can tell, what I have written should work. I’m sure I’m missing something.

  **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) {

  for (let i = 0; i <= contacts.length - 1; i++){
    if (contacts[i]["firstName"] = name){
      if (contacts[i].hasOwnProperty(prop)){
        return contacts[i][prop];
      } else {
        return "No such property";
      }
    }
  }
  return "No such contact";

}

lookUpProfile("Akira", "likes");
console.log(lookUpProfile("Kristian", "lastName"));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36

Challenge: Profile Lookup

Link to the challenge:

Do you want to set contacts[i]["firstName"] to name or do you want to compare those two variables?

1 Like

Side note, while technically correct, this is a somewhat strange way to write this.

Also note, Java and Javascript are very different.

1 Like

I just realized what I did wrong. Should have been ==. Thank you

Or strict equality ===

I think if you get in the habit of using strict equality you will find in time that you are much less likely to use = by mistake. Although it can certainly still happen.

1 Like

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