What exactly do you think the above is checking? If you want to check if an object as a property, you can simply write it like:
var obj = {
someProperty: 'a value',
someOtherProperty: 'another value'
};
// check if obj has a property named 'someProperty'
if (obj.hasOwnProperty('someProperty')) {
console.log('it does');
}
// The above check will be true, so 'it does' will be displayed in the console.
A previous comment sent me to a web that mentioned that " JavaScript does not protect the property name hasOwnProperty ; thus, if the possibility exists that an object might have a property with this name, it is necessary to use an externalhasOwnProperty to get correct results:". Which makes sense since the program might run on the basis that hasOwnProperty is a property were trying to access through the dot notation used to access properties.
Also the directions ask to check if it doesn’t have the property
That could be said about almost all JavaScript object/array methods and properties. There is no reason to do that for this challenge. Just stick with the built-in hasOwnProperty method and you will be fine.
var obj = {
someProperty: 'a value',
someOtherProperty: 'another value'
};
var propNameToCheck = 'someNonExistentProperty';
// check if obj has a property with same name as the value of propNameToCheck
if (!obj.hasOwnProperty(propNameToCheck)) {
console.log('it does not');
}
// The above check cause 'it does not' to be displayed in the console.
{
'1245': {
artist: 'Robert Palmer',
tracks: []
},
'2468': {
album: '1999',
artist: 'Prince',
tracks: ['Free']
},
'2548': {
album: 'Slippery When Wet',
artist: 'Bon Jovi',
tracks: ['Let It Rock', 'You Give Love a Bad Name']
},
'5439': {
album: 'ABBA Gold'
}
}
Your latest version causes the above collection object to be returned for the following test case:
updateRecords(2468, "tracks", "Free");
You have two problems in the section of code below:
} else if (prop == "tracks"){
if (({}).hasOwnProperty.call(collection, prop)== false){
collection[id][prop] = [value];
} else if (value != ""){
Collection[id][prop].push(value);
}
}
If prop is “tracks”, which it is in the test case above, your nested if statement is asking if there is NOT a property named “tracks” in the collection object and this evaluates to true. There is no “tracks” property in the collection object. Instead, there are property names like ‘1245’, ‘2468’, ‘2548’, and ‘5439’.
Also, if the very next else if statement evaluates to true at any point, you are going to get an error, because Collection is not defined anywhere in your code.
Thanks, that makes sense. I’ll check the internet to look for ways to make it so that it evaluates the property inside of the id object.(checking for properties in nested objects)
I also fixed the capitalization error.
oohhhhhhh, I could just see if the result of collection.[id][prop] yields undefined. I was testing for nested objects instead of checking for undefined. wow
Thank you so muchhhh. I was stuck on that forever.