Record Collection challenge is proving a real challenge to me

Tell us what’s happening:
The reason given for the failure of my code is, “Cannot read property ‘pop’ of undefined”. I didn’t trigger the pop command in this exercise and `I’m at a loss regarding what the ‘pop’ refers to.
Could someone kindly explain what is happening here and where my error is coming from?
I am taking a break now and will pick up any responses later in the day. Many thanks in advance for coming to my aid.
// 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;
  } else if (prop === "tracks" && value !== ""){
    if (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(5439, "artist", "ABBA");


**Your code so far**

```js

// 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;
  } else if (prop === "tracks" && value !== ""){
    if (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(5439, "artist", "ABBA");

// running tests
Cannot read property 'pop' of undefined
// tests completed
You may refer back to [Manipulating Complex Objects](https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/manipulating-complex-objects) Introducing JavaScript Object Notation (JSON) for a refresher.

Run the TestsReset All Code[Get a hint](https://guide.freecodecamp.org/certifications/javascript-algorithms-and-data-structures/basic-javascript/record-collection)Ask for help

Passed

After  `updateRecords(5439, "artist", "ABBA")` ,  `artist` should be  `"ABBA"`

After  `updateRecords(5439, "tracks", "Take a Chance on Me")` ,  `tracks` should have  `"Take a Chance on Me"` as the last element.

Passed

After  `updateRecords(2548, "artist", "")` ,  `artist` should not be set

Passed

After  `updateRecords(1245, "tracks", "Addicted to Love")` ,  `tracks` should have  `"Addicted to Love"` as the last element.

Passed

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

Passed

After  `updateRecords(2548, "tracks", "")` ,  `tracks` should not be set

Passed

After  `updateRecords(1245, "album", "Riptide")` ,  `album` should be  `"Riptide"`

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36.

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

Use Else if only to test a specific condition different from any other else if or if statements and only if at the end you have an else statement containing the code to execute in case non of the previous statement has evaluated true.
You can simplify your code in distinct if statements.

If "value" is empty ( "" ), delete the givenpropproperty from the album.
in your code
else if (value === "") { delete collection[id][prop]; }
could execute until this line and evaluate true in may different conditions not only in the requested one.

You are really REALLY close. Only one branch missing in all your logic. I ran your code, adding the missing piece and changing nothing else, and it passes.

So… what are you missing? Well, inside the check for tracks with a value, you check if there IS a tracks property, and if there is, you push the value onto that. Great. So what do you do if there isn’t a tracks property? Currently, that case sort of… fizzles.

In that if statement, that checks if the given collection[id][prop] (tracks) actually exists, just add an else. That should get it done.

Hi
I’ve just come back to this challenge and have noted your advice. But what should the else statement say? I can’t figure this out from the information provided in the challenge?

in the else statement you need to do something when the array doesn’t exist - so if it doesn’t exist, what you do?

1 Like

To answer the question, I need to create an empty array if one does not exist.

Ok, thanks everyone that has shone some light in my path. I am beginning to understand what you are saying!

1 Like