'Profile Lookup' challenge, help needed

Hey, I was able to complete the challenge, but I had trouble with one specific line at one ‘if’, first let me show you my code:

// 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"],
  },
];
let i = 0;

function lookUpProfile(name, prop) {
      console.log("run #" + i);
    if (contacts[i].firstName == name){
      //console.log("confirmed name");
      console.log(contacts[i].hasOwnProperty(prop));
      if (contacts[i].hasOwnProperty[prop] = true){
      //return contacts[i][prop];
      return console.log(contacts[i][prop]);
      }
      else{
      //return "No such property";
      return console.log("No such property");
      }
    }
    else{
    //console.log("name not localized");
      if (i < contacts.length - 1){
      i++;
      //console.log("running function again\n");
      return lookUpProfile(name, prop);
      }
      else{
      //return "No such contact";
      return console.log("No such contact");
      }
  }
}
//lookUpProfile("Kristian", "lastName"); //DONE
//lookUpProfile("Sherlock", "likes"); //DONE
//lookUpProfile ("Harry", "likes"); //DONE
//lookUpProfile("Bob", "number"); //DONE
//lookUpProfile("Bob", "potato"); //DONE
//lookUpProfile("Akira", "address"); //DONE

Of course if ran on the freeCodeCamp editor it would show all errors due to me using console.log in each return, that is because I was testing what was going on each time I was running the function with the suggested parameters

Okay, moving on to the problem, it was at this specific line:

      if (contacts[i].hasOwnProperty[prop] = true){
      //return contacts[i][prop];
      return console.log(contacts[i][prop]);
      }

Whenever I ran “lookUpProfile(“Kristian”, “lastName”)” it would show me its respective value “Vos”, but when running “lookUpProfile(“Akira”, “address”)” it would show that the value returned is ‘undefined’.
When I set the condition to two equals it would ran the second function with no problem.
I ran both separately, setting all of the functions as comments excluding the one that I’m testing, in this case being “Akira”, “address”.

Hi there, I couldn’t find your actual question above?
(Just comments)

I hope you know that one equal sign is called an assignment operator and is not to be used as a comparison operator (== or ===)

1 Like

This is the block of code that I had trouble with, I took your feedback into consideration and replaced the assignment operator with an equality operator, later I found out I had made a mistake with the ‘.hasOwnProperty()’ property, as I encased the “prop” value with brackets instead of a parentheses.

if (contacts[i].firstName == name){
//Replaced [prop] brackets to (prop), as well as replacing the singular equal to an equality operator "=="
      if (contacts[i].hasOwnProperty(prop) == true){
      return contacts[i][prop];
      }
      else{
      return "No such property";
      }
}

Thanks for the assistance!

1 Like