Record Collection, .hasOwnProperty and brackets

Record Collection, .hasOwnProperty and brackets
0

Hi there.
So the Record Collection challenge hey,… that was a tough one, and frustrating to.
I kept being so close, and on the right lines, but missing little bits that was throwing me off. After a goodly while i relented and check in hints. Discovered the concept of an if/else inside an if, but was expecting to use the .hasOwnProperty method to check if “tracks” was there. I can’t seem to make that work, and the

if (collection[id][prop])

bit seems to do that job. Could someone clarify that for me please.
Aside from that i was happy i was on the right lines, and managed to carry on unaided to clear the deleting bit. But the real stumper seemed to be that i couldn’t achieve what i thought was the easy bit, just adding values in.
early on I’d accepted that i need to use[]since Function parameters are variables,… and that was reinforced by the else in the if/else within the first if,…

 else {
      collection[id][prop]=[value];

But for some reason, i couldn’t get it to add values in normal the rest of the time. Again, relented to the hints to discover i had to loose the brackets around the value.
And thats had me really stumped. Could anyone help explain why in that instance, it would fail when valuable was in[] please.

 else  {
  collection[id][prop]=value

Here’s the full code i’ve had working and passing all the tests. I’m just hesitant to move on until i really understand these quirks.
Thanks

// Only change code below this line
function updateRecords(id, prop, value) {
  if (prop === "tracks" && value !== "") {
    if (collection[id][prop]){
      collection[id][prop].push(value);
    }
    else {
      collection[id][prop]=[value];
    }
  }
else if (value === "") {
  delete collection[id][prop]
}
else  {
  collection[id][prop]=value
}
  return collection;
}

// Alter values below to test your code

here you are creating an array, and putting value inside it. but most properties on the objects do not have a value of array

1 Like