Record Collection Exploring The Question

Record Collection Exploring The Question
0

#1

I don’t have any code yet. I’m trying to understand the problem first. This confuses me:
If value is non-blank ( value !== “” ), then update or set the value for the prop .

How do I update or set a value for prop?

Do add value to prop?

Tell us what’s happening:

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) {
  
  
  return collection;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

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


#2

This would be setting a property

//in the collection before
"1234": {
      "album": "Raw Power"
    }

updateRecords(1234, "artist", "The Stooges");

//in the collection after
"1234": {
      "album": "Raw Power",
      "artist": "The Stooges"
    }

An update would change the existing property in some way.

The instructions of this one can be a little tricky. There’s no dishonor in using pen and paper for a quickie flow chart to work out your logic.


#3

" If value is blank, delete that prop .",

Does that mean if tracks does exist delete the prop anyways? Or just delete the user’s input?


#4

The function’s purpose is to make changes to the record collection object.


#5

This is kind of redundant. Why use if twice. Why not just return collection[id[prop].push(value);

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

#6

Also in:

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

  return collection;

Why is there an else statement after the first if statement.


#7

Because the push method has it’s own return value and it is not an array. Read about the push method’s return value.


#8

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution.