Record Collection: Run the Tests gives an error. My console does not

Record Collection: Run the Tests gives an error. My console does not
0

#1

I struggled to get a solution to Record Collection but (after having a quick peek at the hints) get something which runs in my browser console and seems to make all the required changes to the record collection.

When I ‘Run the Tests’ I get an error:

updateRecords(...)[5439].tracks.pop is not a function

I do not even use .pop in my code so this is confusing me.

My solution to the problem is to change the updateRecords() function to the the following:

function updateRecords(id, prop, value) {
  if (prop === "tracks") {
      if (collection[id][prop]) {
          if (value !="") {
            collection[id][prop].push(value);
          } else {
              delete collection[id][prop]
          }
      } else {
          collection[id][prop] = value;
      }
  }
  if (prop === "artist" && value != "") {
      collection[id][prop] = value;
  } 
  if (prop === "artist" && value ==="") {
    delete collection[id][prop];
  }
  if (prop === "album") {
      collection[id][prop] = value;
  }
  
  return collection;
}

I would really appreciate knowing what is going on.


#2

The problem is here:

if (prop === "tracks") {
      if (collection[id][prop]) {
          ...
      } else {
          collection[id][prop] = value; // this line
      }
  }

If collection[id][prop] doesn’t exist, you need to set it up as an array, not a string (value). The pop function is probably called later by the test suite, which won’t work on a string.


#3

Thanks. That really shed some light on it. All I had to do was put square brackets round value to give [value] so it creates an array not a string. On to the next challenge…


#4

That’s right. The error in this case is actually very simple to solve. Spotting it, however, is where the challenge lies.