Basic JavaScript - Profile Lookup

Tell us what’s happening:
Describe your issue in detail here.

Hello I was just wondering why the && operator doesn’t work here?

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 i = 0; i < contacts.length; i++) {
      if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
        return contacts[i][prop];
      } else {
        return "No such property";
      }
    }
  // 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/116.0

Challenge: Basic JavaScript - Profile Lookup

Link to the challenge:

Haw would you use && here?

I don’t think this code you gave passes the challenge?

if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
        return contacts[i][prop];

It does not pass the test, but I was wondering why that is?

You certainly can use && there. Your problem is not with using &&

return immediately stops the function, preventing any more code from running, to include stopping a loop before it finishes all iterations.

are you referring to return “No such contact”?

Any and every return statement stops all further code execution in a function call immediately.

But yeah, having return "No such contact" in the middle of the loop is equivalent to saying “if you find someone that doesn’t match, give up, stop looking further, and assume you never will find anyone that matches”.

Thank you for your help.

To help me understand better I inputted the data to chatGPT. I couldn’t understand why using the && operator combined with return contacts[i][prop] || "No such property" wasn’t working. I am assuming this is because the && operator is checking to see if both statements are true at the same time and not picking up the "No such property" statement. It is suggested that a second if statement is used instead.

This is a great example of ChatGPT sounding really convincing but being totally wrong. Gotta take what it says with a huge grain of salt.

Now you do need to handle the case where the contact matches and they don’t have the property. There are many ways to write that logic.

I wouldn’t necessarily say it was chatGPT more like my wording and lack of understanding. I agree though I do try to to understand what is happening outside of chatGPT. I mostly use it has an assistant.

I think I finally understand what you are trying to convey to me. I believe it is just the test itself that needs to be written a certain way in order to pass, but as far as writing the code another way, their are other possibilities. Also, I understand from what you mentioned about return that a second if statement is needed so that the first if statement can continue running if the second statement returns “No such property”.

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