Record collection understanding the exercise

What do you get when you try exactly what I pasted?

Which output has the property albumTitle? The first one or the third one?

the first one

console.log(records[id])

Ok. So why would you check if records[id][prop] has the property prop? That is, literally, asking if the property has the property.

function updateRecords(records, id, prop, value) {
  console.log("records[id] =");
  console.log(records[id]);

  console.log("\nprop =");
  console.log(prop);

  console.log("\nrecords[id][prop] =");
  console.log(records[id][prop]);

  console.log("\nrecords[id].hasOwnProperty(prop) =");
  console.log(records[id].hasOwnProperty(prop));

  console.log("\nrecords[id][prop].hasOwnProperty(prop) =");
  console.log(records[id][prop].hasOwnProperty(prop));
}

updateRecords(recordCollection, 5439, 'albumTitle', 'new title');

but prop has two properties : tracks and artist. so dont i have to select one from two properties?
thank you in advance for helping

the variable prop can be any value, but it is only a value at a time - it can have different values in different function calls, but inside a single function call its value is always the same
the instructions tell you to do one thing if prop is equal to "tracks" and a different thing if it is different

but hasOwnProperty can’t be used to check the value of a variable, it’s used to check if an object has a property or not
you can use ‘hasOwnProperty’ only on an object, not on other things, if you use it on other things you get an error and the code doesn’t execute

1 Like
console.log(records[id][prop].hasOwnProperty(prop));
output: false

but it is working

please do console.log(records[id][prop]) to see if that is an object, because it is not, and it’s not what you need to check

1 Like

This does not work. Do not do this. The property prop will not itself have the property prop. records[id][prop] is not an object, so it does not have properties. That’s what the whole console logging example above showed.

records[id][prop].hasOwnProperty(prop);
it is showing false. what does it mean?

I don’t think it’s possible to help you if you don’t follow what has been recommended, and console.log what you’re dealing with.

This is records:

{
2548: {
  albumTitle: 'Slippery When Wet',
  artist: 'Bon Jovi',
  tracks: ['Let It Rock', 'You Give Love a Bad Name']
},
2468: {
  albumTitle: '1999',
  artist: 'Prince',
  tracks: ['1999', 'Little Red Corvette']
},
1245: {
  artist: 'Robert Palmer',
  tracks: []
},
5439: {
  albumTitle: 'ABBA Gold'
}
};

This is records[id] when id is 2548:

{
  albumTitle: 'Slippery When Wet',
  artist: 'Bon Jovi',
  tracks: ['Let It Rock', 'You Give Love a Bad Name']
}

This is records[id][prop] when id is 2548 and prop is tracks:

['Let It Rock', 'You Give Love a Bad Name']

As you can see, it’s an array. It has no property prop.

i am sorry it might be a misunderstanding.
i have console and has seen that -

records[id][prop] 

is not an object.
and

hasOwnProperty();

cannot be used with it. just want to know what does the output false means here?

thank you for helping. i am a bit slow at catching things sorry for that

Please don’t take this the wrong way but it may be worth going back to some previous challenges and make sure you understand well the concepts introduced in them.

If you continue with the challenges without satisfactory understandings of some of the fundamentals, you may find them more and more confusing and burn out very quickly.

1 Like

If records[id][prop] is an array, then it is an object. I hope this is not too confusing. Everything in JavaScript is either a primitive value (a number, a string, …) or an object. An array is just a special kind of object.

That’s why you can use hasOwnProperty on it. By returning false, it’s telling you that the array has no such property. You could however check if the array has a property called length:

console.log(records[id][prop].hasOwnProperty('length'))

That would return true, because every array has a length property.

2 Likes

That’s why i was asking ,
i mean how is it possible it is comparing or giving a feedback for the hasOwnProperty(). thank you for the answer
so basically it is a array and array is a special type of object. that’s why it is working but as array can’t have a property that’s why it is returning false

Hey I don’t mean to confuse you further, but just so you wouldn’t have the wrong impression about arrays in general, I just want to point out that arrays can have properties. AAMOF all arrays have the length property, and you can also add properties to them (not that you’ll need to or should most of the time):
Snipaste_2021-05-20_15-22-45

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