Not very confident on Record Collection. Am I even doing the first part correct?

I’m confused on this challenge as it’s like all over the place in my head. I had to delete my last code as I’m sure I was doing it all wrong. I’m not sure if I’m doing the first part correct. The part where it says .

“If prop isn’t “tracks” and value isn’t empty (”"), update or set the value for that record album’s property."


// 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 !== "") {
  var deleteProp = delete collection[prop];
  var addValue = collection[prop].push(value);
  return addValue;
}

// 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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

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

Posted this on Reddit as well, but here’s the same thing in case you don’t see it:

When I tried to run your code, I got this (you’re also missing a } after the function declaration):

TypeError: Cannot read property 'push' of undefined
    at updateRecords:34:37
    at eval:40:1
    at eval
    at new Promise

Here’s your function:

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

I think what you’re trying to do is to delete the prop, then push the value to where that prop used to be. The bad news is that you’re overcomplicating it with the wrong methods, but the good news is that you’re close and the answer is simpler!

If you want to overwrite a value, just assign it! For instance, let’s say I have an object:

var foo = { bar: 2 }

If I want to update the bar value, I don’t have to delete it! I can just do this:

foo.bar = 3;

// Or I could do foo['bar'] = 3;

console.log(foo)
> {bar: 3}

push is for arrays, so it’s not something you have to worry about it here!

Okay so I changed it to this. Should I move on or am I missing something? I haven’t got any of the stuff ticked off on the list either.


function updateRecords(id, prop, value) {

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

Sorry I was away but I just came back and did what you said and I did end up getting some of the task right. Only 3/7 though. Here’s what I wrote.

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

Two things:

  1. You were very close above, but you’re back in trouble. Look at this section:
if (prop !== "tracks" && value === "") {
    collection[id][prop].push(value);

What are you trying to do here? Write it out in English, then write out what your code is doing.

You’re still trying to push for something that’s not an array (as I mentioned above, you don’t have to push, just reassign!), and you should double check your conditionals.

  1. When you’re trying to see if the tracks array doesn’t exist, you don’t quite have it right. If the tracks key doesn’t exist, collection[id]["tracks"] will return undefined, not false. If it’s undefined, inside the conditional, you won’t be able to push to undefined; you’ll have to make it an empty array, then push. Alternatively, you could just assign it to be an array with the one value.

I’m sorry I wasn’t even thinking straight when I typed that code out. I didn’t mean to change the !== to === just I was basically panicking figuring out what will work. Anyways I’m starting fresh again and I’m a little confuse about what I should assign what to? Sorry if I’m being a pain. If I could get the first part correct then that would dramatically improve my confidence on this.

never mind got it correct now I’m going to attempt the rest