Record Collection Help - Incorrect Conditional Order?

Hi there, I’m working on the ‘Record Collection’ problem and am a bit stuck
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection

I’m currently getting the following errors:

// running test
After updateRecords(2468, "tracks", "Free"), tracks should have "1999" as the first element.
After updateRecords(2548, "tracks", ""), tracks should not be set

This is my code so far. It seems to be working as expected locally, but when I transfer the code to FCC, the tests mentioned above fail. I suspect that it’s my ordering of the conditional statements that is causing the failing tests.

var collection = {
    "2548": {
      "album": "Slippery When Wet",
      "artist": "Bon Jovi",
      "tracks": [ 
        "Let It Rock", 
        "You Give Love a Bad Name" 
      ]
    },
    "2468": {
      "album": "1999",
      "artist": "Prince",
      "tracks": [ 
        "1999", 
        "Little Red Corvette" 
      ]
    },
    "1245": {
      "artist": "Robert Palmer",
      "tracks": [ ]
    },
    "5439": {
      "album": "ABBA Gold"
    }
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

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

  if (prop !== 'tracks' && value) {
    collection[id][prop] = value;
  } else if (prop === 'tracks' && !collection.hasOwnProperty('tracks')) {
    let tracksArray = [];
    tracksArray.push(value);
    collection[id][prop] = tracksArray;
  } else if (prop === 'tracks' && value) {
    collection[id][prop].push(value);
  } else if (!value) {
    delete collection[id][prop];
  }
  return collection;
}

Please let me know your thoughts! I’m just looking for a bump in the right direction and not the actual solution. Many thanks!

After updateRecords(2468, “tracks”, “Free”), tracks should have “1999” as the first element.

You are failing this test case because after these values are passed in. The only item in tracks array is “Free”

2468: { album: ‘1999’, artist: ‘Prince’, tracks: [ ‘Free’ ] },

1 Like

Thank you for you reply! I still couldn’t get it to work yesterday, but after clearing my mind I was able to correct my mistakes. Here is my working code for anyone that is interested:

My issue was indeed my ordering of the conditional statements. As mentioned by shimphillip, I was overriding the existing track in the tracks array, because my conditional, else if (prop && value) { collection[id][prop] = value; came before my conditional else if (prop === 'tracks' && value) { collection[id][prop].push(value); that would push an additional track.

function updateRecords(id, prop, value) {
  if (!value) {
    delete collection[id][prop];
  } else if (prop === 'tracks' && !collection[id].hasOwnProperty('tracks')) {
    let tracksArr = [];
    tracksArr.push(value);
    collection[id][prop] = tracksArr;
  } else if (prop === 'tracks' && value) {
    collection[id][prop].push(value);
  } else if (prop && value) {
    collection[id][prop] = value;
  }
  return collection;
}