Can't find the error

Can't find the error
0.0 0

#1

Tell us what’s happening:
I can’t find the error in my code. Out of the 7 test cases, 5 have passed but 2 are failing.

Your code so far


// Setup
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;
    return collection;
  }
  else if(prop=="tracks" || !("tracks" in collection[id])){
    collection[id][prop]=[value];
    return collection;
  }
  else if(prop=="tracks" && value!=""){
    collection[id][prop][prop.length]=value;
    return collection;
  }
  else if(value==""){
    delete collection[id][prop];
    return collection;
  }
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection


#2

I will give you a quick hint. If you look at the tests that are failing, they are both related to “tracks”. This is because of the following part in your code:

else if(prop=="tracks" || !("tracks" in collection[id])){
    collection[id][prop]=[value];
    return collection;
  }

This code checks for “tracks” and equals tracks to the value. When the value is empty, an empty value is put next to it instead of deleting it. You included the delete empty strings logic in your code at the end of the if else section. But this is never triggered as your if else stops in the above part. The other failing test is because you are replacing the already existing array of tracks with just your value. You should ADD it to the already existing list of tracks :slight_smile:.

I hope this helps