Record Collection-Passing All Tests Except For 1

Record Collection-Passing All Tests Except For 1
0

#1

Hello. The following code is passing all tests except for this one below. Not sure how to pass it. Many thanks in advance.

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

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

// Alter values below to test your code
updateRecords(2468, "tracks", "Free");

#2

@ksirgey,
after reading the guidelines for this challenge and then looking at your code, it seems you are missing a rule for handling incomplete data.

Take a look at the following two rules:

If prop is “tracks” but the album doesn’t have a “tracks” property, create an empty array before adding the new value to the album’s corresponding property.

If prop is “tracks” and value isn’t empty (""), push the value onto the end of the album’s existing tracks array.

And your code below:

if (prop !== "tracks" && value !== "") {
    collection[id][prop] = value;
  }
  
  else if (prop === "tracks" && value !== "") {
      collection[id][prop] = [];
      collection[id][prop].push(value);
    }

You are always creating an empty array for the tracks property, even though you shouldn’t. Only when the property is tracks, but there is no value for it, you should create a new array.