// Only change code below this line
function updateRecords(id, prop, value) {
var newID = id.toString();
console.log(prop);
if (prop !== 'tracks' && collection.newID.prop === false ) {
collection.newID = prop;
collection.newID.prop = value;
} else if (prop !== 'tracks' && collection.newID.prop.value !=='') {
collection.newID.prop = value;
} else if (prop === 'tracks' && collection.newID.tracks == '') {
collection.newID.prop = [];
collection.newID.prop.push(value);
} else if (prop === 'tracks' && collection.newID.tracks.isArray) {
collection.newID.prop.push(value);
} else if (collection.newID.value === '') {
delete collection.newID.prop;
}
return collection
Sorry if this has been asked before - I am looking through similar topics and cannot find a post that mirror this one, mostly because I do not know enough about the problem to know what to search for when looking at other FCC posts about this exercise.
Before posting this I tried:
using console.log(newID) to make sure that newID was converting the number to a string before Javascript function applied that argument to the object.
to try and ensure that if prop is undefined (as per the error message noted above) then the function would assign a new prop key within the object and then assign the function’s value to that key. But this doesn’t work. After I coded this in and ran the function I still received the error message `Cannot read property ‘prop of undefined’
To access props you need to check indexes, not write it as collection.newID.tracks.
Example, to look up the id in a the collection you should go by collection[id] to get the matching object-index by id, then accessing it’s props with [props], which you can do directly like this, collection[id][props].
One can think of an object as an associative array (a.k.a. map, dictionary, hash, lookup table). The keys in this array are the names of the object’s properties.
You may also want to give the for…in loop a look, it may provide some clues.
var newID = id.toString();
console.log(prop);
if (prop !== 'tracks' && collection.newID.prop === false ) {
...
So NewID is used to access the array’s index number(in this case i.e. the test, 5439, which is coded in the array as a string, ‘5439’ - hence id.toString(); )
I don’t understand your feedback I am afraid. Could you please clarify?
I have read the challenge on bracket notation again. I can access object keys via dot notation, which is what I have used. So why should I use bracket notation instead?
This is undefined, because dot notation access literally a key newID which doesn’t exist. Bracket notation evaluated what’s inside the brackets before accessing the property.