[Record Collection] Not sure why my code doesn't work

[Record Collection] Not sure why my code doesn't work
0

#1
// Only change code below this line
function updateRecords(id, prop, value) {

  var album = collection[id];
  
  // Empty value param: remove the prop
  if (~value) {
    delete album[prop];
  }
  
  // Update tracks list
  if (prop === "tracks") {
    if (value) {
      if (~album.hasOwnProperty('tracks')) {
        album.tracks =[];
      }
      album.tracks.push(value);
    }
  }

  // Update anything else
  else {
    if (value) {
      album[prop] = value; 
    }
  }

  return collection;
}

The above is my solution for the Record Collection challenge but it seems to fail the test for
updateRecords(2468, "tracks", "Free")
for which tracks should have 1999 as the first element.

When I debugged, it seems like the line:
album.tracks =[];
is being run even though the id 2468 has a property called tracks and the conditional should be preventing the above from running.


#2

Hi there,

The takeaway here should be to not use bitwise operators where a simple and conventional ! would suffice. In this case, if (~value) is causing problems because even after having its bits flipped, value is still truthy. Otherwise, it works, and well done!


#3

Oh, right. For some reason, an online reference led me to believe the ! operator from other languages is replaced by the ~ operator in JS but it’s actually the same. Thanks!