Record Collection error

Record Collection error
0.0 0

#1

Hey guys,

I’ve been working on the Record Collection challenge and here is my code:


// Setup// Only change code below this line
function updateRecords(id, prop, value) {
  var record = collection[id];
  if (prop == "tracks"){
    if (record["tracks"] == ""){
record["tracks"] = [];
    }
    record["tracks"].push(value);
  }
  if (value ===""){
    delete record[prop];
  }
else if (prop !== "tracks"){
  record[prop] = value;

}
  return collection;
}

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

Problem is, I get an error saying “Cannot read property ‘push’ of undefined” when I run the tests.
Not sure what this means? I thought it was clear in my code that I am pushing value to the end of the record[“tracks”].

Can anyone help? Thanks in advance!


#2

If the property "tracks" doesn’t exist, it won’t be an empty string. This condition won’t be satisfied and record["tracks"] is undefined when you try to push to it.


#3

added a condition that checks if prop “tracks” exist at all (hasOwnProperty), worked for me.


#4
function updateRecords(id, prop, value) {
  if(value!=="")
  {
  if(prop!=="tracks")
    collection[id][prop]=value;
  else if(prop=="tracks"){
   if(!collection.hasOwnProperty(prop))
   {
     var tracks=[];
      collection[id][prop]=tracks;
   }
      collection[id][prop].push(value);
  }
  }
  else if(value==="")
    delete collection[id][prop];

  
  return collection;
}

Error in:
After updateRecords(2468, “tracks”, “Free”), tracks should have “1999” as the first element.
Please suggest.


#5

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#6

Got it figured out. Thank you guys for your help!