Record Collection exercise problem

Tell us what’s happening:
No idea what the problem can be since there’s barely any debugger :frowning:

Your 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 (collection.id.prop !== "tracks" && collection.id.prop.value !== ""){
    collection.id.prop.value = value; //value updated
  }else if(collection.id.prop == "tracks" && collection.id.prop.value === undefined){
    collection.id.prop = []; //empty array created
    var arr = collection.id.prop; 
    arr.push(value); //new value to the album corresponding property added
  }else if(collection.id.prop == "tracks" && collection.id.prop.value !== ""){
    var arr = collection.id.prop;
    arr.push(value); //value pushed onto the end of the almubs existing tracks array
  }else if(collection.id.prop.value == ""){
    delete collection.id.prop; //prop deleted
  }
// 
  
  
  return collection;
}

// 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/66.0.3359.181 Safari/537.36.

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

What is the difference between accessing a property with a dot operator versus using bracket notation?

2 Likes

Still struggling with this, although I think I’ve noticed the dot and bracket difference.

function updateRecords(id, prop, value) {
  if (collection.id.prop == ""){  //If value is empty (""), delete the given prop property from the album.
    delete collection[id][prop];
  }
  
  else if( collection[id][prop] == "tracks" && collection.id.prop != "" ){
    collection.id.prop.push(value);  //If prop is "tracks" and value isn't empty (""), push the value onto the end of the album's existing tracks array.
  }
  
  else if (collection[id][prop] == "tracks" && collection.id.prop === undefined){ //If prop is "tracks" but the album doesn't have a "tracks" property, create an empty array before adding the new value to the album's corresponding property.
    collection.id.prop=[];
    collection[id][prop].push(value);
  }
  
  else if (collection[id][prop]!="tracks" && collection.id.prop != ""){ //If prop isn't "tracks" and value isn't empty (""), update or set the value for that record album's property.
    collection.id.prop = value; 
  }
  
  return collection;
}

You changed some dot notation to bracket notation, but not all. What’s that based on?

well, Im assuming that

//considering the last object

collection.id.prop //returns "ABBA Gold"
collection[id][prop][value] //returns "ABBA Gold"
collection[id][prop] //returns "album"

I see no reason to put it all in bracket notation, althought I just did it, and it still doesnt compile (Same order as previous answers)

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

This tells me that you didn’t either didn’t review the difference between the two notations or that you didn’t understand it. It also tells me that you didn’t test it.