hasOwnProperty check error

Tell us what’s happening:

Your code so far

// Setup
var 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
var pr,con=0;
for(let i=0;i<contacts.length;i++){
if(contacts[i]["firstName"] == name)
{   contacts[i][prop] = contacts[i][prop] || [];
    if(contacts[i][prop] != []){return contacts[i][prop];}
    else{pr = "No such property";}}
else{con +=1;}
if(pr === "No such property"){ return pr;}
else if(con == contacts.length){return "No such contact";}
// Only change code above this line

lookUpProfile("Akira", "likes");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36.

Challenge: Profile Lookup

Link to the challenge:

Hi @dibyajyoti.samal,
Welcome to the freeCodeCamp forum!

What did you expect to happen? What did you observe instead?

im not getting output of “no such property”. i dont know what i did wrong
i know the code is messy. i just started few days ago

you can’t compare an array like this, but also none of the properties are arrays
what do you want to check with this?

why this? in no case you need to create an array if the property is not there, there is any reason for which you are doing this? you are not asked to change the object

1 Like

Code formatted:

function lookUpProfile(name, prop) {
  // Only change code below this line
  var pr,
    con = 0;
  for (let i = 0; i < contacts.length; i++) {
    if (contacts[i]["firstName"] == name) {
      contacts[i][prop] = contacts[i][prop] || [];
      if (contacts[i][prop] != []) {
        return contacts[i][prop];
      } else {
        pr = "No such property";
    } else {
      con += 1;
  if (pr === "No such property") {
    return pr;
  } else if (con == contacts.length) {
    return "No such contact";
  // Only change code above this line

i made a new property in the object with that name, to help check if property exists or not. Makes a blank array if the prop doesn’t exist , remains same if there is a value before

i use contacts[i][prop] to access the object property , it can be done by using dot (’ . ') ,or square brackets.

// running tests

"Akira", "address"

should return “No such property” // tests completed

this is the error that was/is showing. i really have no idea what i’m doing wrong

Check this again… This will help you
Basic JavaScript: Testing Objects for PropertiesPassed

1 Like

this is correct, but you are using [contacts][i][prop] which is not correct

why do you want to do that? you do not need to change the property to check if it is exist. you have hasOwnProperty to check

what should i write in place of [contacts][i][prop] …??

And ,yes i wrote long code unnecessarily, i had forgotten the functioning of return :slightly_frowning_face: .
thanks for helping. I’m trying to correct it

it was a helpful indeed. :innocent:

you wrote it yourself what is it that you should use

u said it was not correct so i asked…

[contacts][i][prop] is not the same thing as contacts[i][prop]

one is correct, the other is not

i get what you are tryin to say…
i was confused as to where i made a mistake. Actually, if u see above replies , u will find that i have never used [contacts] , neither in the code nor in the replies. :sweat_smile: and when i saw that in your replies, i thought it might be some error in the webpage.
U can see it was u who said it first.
But , thanks a lot for helping a newbie :slightly_smiling_face:

you are right, I don’t know where I saw it. I am not surprised if I inverted two answers for different questions on the same challenge…

but anyway, you do not need to change the object, even, you should not change the object.

Also, the contacts[i][prop] != [] will not work as you want

yes, changing an object for your application does seem unprofessional. and might cause problems afterwards. i got the solution , thank you friend

I think I haven’t seen any explanation of this bit so here goes:
arrays are passed by reference (look it up if you don’t understand, it is one of the basics in JS and many programming languages - passed by reference vs passed by value).

In other words, when you compare anything to [] (new empty array) you will always get false. This is because arrays are passed by reference. In other words, you are comparing a new empty array reference to some existing value. Since you’ve just created that reference, nothing can be equal to it.

If you want to check if something is array, there is a handy prototype method:
This function returns true if this is an array and false otherwise.

Also, I highly recommend not using == or !=, it is better to use === or !== which checks for both type and value. If you want to convert a value from one type to another type, it’s best to do it explicitly.

Hope this helps!
Have a nice day!

WoW… thats a big help. i didn’t know about that. Keep up the good work. Thank you

1 Like