[SOLVED] Record Collection Stuck - Could Need Some Guidance :-)

Hey guys!

I am a super new to freeCodeCamp & coding as I have decided that I want to get over my high school trauma that coding is only for a certain type of person, and that I am not one of them :slight_smile:

Hope you can help me out with the “Basic JavaScript: Record Collection” challenge.

My code so far


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

// Alter values below to test your code
console.log(updateRecords(5439, "tracks", "ABBA"));

When I run the test I get these error messages(depending on input):

Cannot read property ‘push’ of undefined
After updateRecords(2548, “artist”, “”), artist should not be set
After updateRecords(2548, “tracks”, “”), tracks should not be set

What have I done wrong? What should I consider?

Thank you so much!

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

If the object doesn’t have a tracks property this is executed, you need first to create the track property and the array and then push, or you will get that error

This doesn’t help - one because only one statement in a if/else if chain is executed - two because here you are checking if the value of the property is an empty array, not if it is not set, maybe you can find this useful:

Hey ieahleen!

Thanks for this.

I have iterated my code to

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

…and now I seem to get stuck with

After updateRecords(2548, “artist”, “”), artist should not be set
After updateRecords(2548, “tracks”, “”), tracks should not be set

If you set a condition you still need to use else if
Then you are accessing the property wrong. When you have variables you always need to use bracket notation. Also, everywhere else you access the property in a different way than here, why have you changed that?

Thanks! I hope I understood you correctly. I didn’t notice that I have switched in between, but I think it happened because I looked up a previous exercise where it was used that way. I changed it now to

   else if (value === "") {
    delete collection[value];
  }

still an error. Am I missing something in the overall logic?

You are trying to access collection[""], does the collection object has a property like that?

You are still doing it differently then the others

Hey!

Ok, with fresh eyes the problem was immediately obvious :smiley: I think I just needed some time off the screen to realise it.

My code is now fulfilling all success criteria of the exercise

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

Thanks @ilenia :pray:

1 Like