0

I am working through the freeCodeCamp tutorial for Javascript, and have been working through the Record Collection assignment. I'm trying the write it differently than is presented, but despite looking through the past tutorials and searching online, I can't understand why my code is not appending the property "artist" to the 5439 object.

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

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

I've looked through the online tutorials and it looks like I am including it properly. I've tried bracket notation as well and nothing has worked but when I debug my code and print the result, nothing is added. Am I just not accessing the object at all?

Broski-AC
  • 739
  • 6
  • 12

3 Answers3

2

You need to use bracket notation when using variables to dynamically access properties in your object collection. When you use a for...in loop, your i variable refers to the keys in your object, so it won't have properties such as .tracks etc... (which means code such as i.tracks.push(value) wont work), you need to get a reference to the object stored at key i using collection[i]:

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

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

However, as all you're doing currently is updating your object, so you don't need a for...in loop as you're providing the id, which acts as the key to update:

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 (id in collection) {
    var to_update = collection[id];
    if (value == "") {
      delete to_update[prop];
    } else if (prop == "tracks") { // only get to this point if `value !== ""`, so there is no need to check `&& value !== ""`
      if ('tracks' in to_update)
        to_update.tracks.push(value);
      else
        to_update.tracks = value;
    } else {
      to_update[prop] = value;
    }
  }
  return collection;
}

// Alter values below to test your code
console.log(updateRecords(5439, "artist", "ABBA"));
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
0

all you just need to do:

  1. use bracket notation
  2. use collection[i][property name] not just i[property name]
  3. when you are adding the track for the first time then add it as an array with a

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

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

single element

0

Always remember that dot notation can be used when the property names are hardcoded not dynamic. Always use bracket notation when you have to access values using dynamic properties.

For more information refer my answer to https://stackoverflow.com/questions/58263221/difference-between-result-id-and-resultid-in-angular-javascript/58263279#58263279 link.

Here there are two issues: 1. for each loop gives you key name for every iteration of an json object but you have used it to access properties from it which is not possible 2. You have used dot notation to access dynamic properties which is wrong. You will have to use bracket notation.

Below is the corrected code:

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

// Alter values below to test your code
console.log(updateRecords(5439, "artist", "ABBA"));
Suneet Bansal
  • 2,664
  • 1
  • 14
  • 18