Sign in
Log inSign up

freeCodeCamp: record-collection exercise

miguelmanalo's photo
miguelmanalo
·May 3, 2020

This exercise is a pain my behind. I had such a hard time getting the tracks checks to work. And the same issue with albums. See the OG link for the instructions. I did learn after my cousin pointed it out that in some parts I wasn't pointing to the sub-property that I had intended to select. I was pointing one level too high.

freecodecamp.org/learn/javascript-algorith…

// 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"
  }
};

// Only change code below this line
function updateRecords(id, prop, value) {
    if (value !== "" && prop == "artist") {
    collection[id][prop] = value;
  } else if (prop == "tracks" && value != "") {
    //Check to see if the given array has the property of "tracks"
    if (collection[id].hasOwnProperty("tracks")){
    collection[id][prop].push(value);
        }   else {
        collection[id].tracks = [];
        collection[id][prop].push(value);
      }
  } else if (value !== "" && prop == "album") {
    //Check to see if the given array has the property of "album"
    console.log("am i here")
    if (collection[id].hasOwnProperty("album")){
    collection[id][prop] = value;
        }   else {
        collection[id].album = value;
      }
  }
  if (value == "") { //if value is blank delete the property
    delete collection[id][prop];
    } 
return collection;
  }

updateRecords(2468, "tracks", "Free");
console.log(collection);