Need help with Record Collection problem

Need help with Record Collection problem
0

#1

Tell us what’s happening:
I dont even know what to ask, this problem came out of nowhere and has so many parameters I dont even know what to do. I even looked at the solution and I’m still puzzled. Can someone please lay this question out step by step so its easier to understand, and help guide me through it?

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 (prop !== 'tracks' && value !== '') {
    collection[id][prop] = value;
  }
  if (prop === 'tracks' && value !== '') {
    collection[id][prop].push[value];
  }
  if 
  
  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/67.0.3396.99 Safari/537.36.

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


#2

Your task is to write a function updateRecords which has 3 parameters, an id, a property, and a value: updateRecords(id, prop, value)

Within this function you write, you must satisfy all of the rules outlined. The general gist of the question is in the first condition:

If prop isn’t “tracks” and value isn’t empty (""), update or set the value for that record album’s property.

So if prop isn’t equal to "tracks" and value isn’t "" then you must set collection[id][props] = value

That’s the question in a nutshell, a function that can update things in collection

The rest of the conditions are handling incomplete or missing data, and telling you what you should do in those cases (especially with tracks as it’s an array and not a single value. You should use conditionals like if to help you with this.

Good luck!


#3

I get what you told me, but im still having problems, do you mind sharing your code so i can pick through it and figure it out. this is really bugging me. Heres my code if you want to take a look. I’m failing one test, which is: After updateRecords(5439, “tracks”, “Take a Chance on Me”), tracks should have “Take a Chance on Me” as the last element.

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

  return collection;
}

#4

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#5

Also how would i create the array if I add a track to an object that doesn’t yet have one?


#6

Your code is not dealing with the following instruction requirement:

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.

Do you not remember how to create an empty array? Assign an empty array first and the push to it.


#7

in the code solution, where is he creating the array? And what condition checks to see if the object doesn’t have a ‘track’ prop?

function updateRecords(id, prop, value) {
  if (prop === "tracks" && value !== "") {
   if(collection<a href='https://forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=3 ":rocket:"' target='_blank' rel='nofollow'>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;
}