Record Collection - can someone just explain the exercise to me?

Having so much trouble with this exercise, but I think a lot of it for me is that I am not understanding the instructions. Could someone explain this exercise to me and what I am trying to accomplish with the code?

2 Likes

Record Collection has a lot of rules, so it confuses a lot of people. Don’t worry, we’ll get you through it.

First off, the essential skill for Record Collection is knowing how to deal with arrays and objects. Make sure you understand how to access object properties with both dot and bracket notation. Also, make sure you know the array.push method.

Accessing object properties in JavaScript: http://www.w3schools.com/js/js_properties.asp

array.push: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

Next, let’s break the problem down into smaller pieces. Basically, according to the instructions, you have two basic situations: one where the ‘prop’ value passed to your function is ‘tracks’ and another where it is anything (including tracks.)

So your pseudo code is something like:

var record = collection[id]; // remember to use bracket notation when referring to a property with a variable
if(prop === 'tracks'){
// check to see if record has a tracks property
// if not, make record.tracks = [];
// push value to record.tracks
}

if(value === ""){
delete record[prop];
}else{
// if property isn't "tracks"
// set record[prop] to value

return collection;

Now obviously that is just pseudo code and you’ll have to fill in some blanks, but hopefully that gets you thinking in the right direction. As long as you know how to refer to object properties in both dot and bracket notation, and you can push to arrays, then you should be okay.

9 Likes

So this pseudo code is super helpful, but I’m also trying to ask for a rephrase of the question so that I understand what the code is trying to accomplish.

The question tells me this:
“Write a function which takes an album’s id (like 2548), a property prop (like “artist” or “tracks”), and a value (like “Addicted to Love”) to modify the data in this collection.”

So I think… takes the prop and val and does what? modifies it how? Like what am I trying to modify the code to do?

It would be so much easier for me to understand if these were phrased in real world ways. I might be able to write the code by following the rules and with the pseudo code you gave me, but the question still does not make sense and I’m not sure exactly what I am writing code to accomplish. Not sure if this makes sense… if I understood the question, I could explain what I mean better, but it seems like a lot of people have had issues understanding the question/ directions also.

thanks!

7 Likes

There’s one collection object. Its properties are also objects, each of which corresponds to a record.
You’re expected to write a function that modifies the records. (it’s not going to add or remove any record, there’re always 4 records).
In order to identify which record to process, the function needs an identification parameter, hence the “id”, 4-digit number.
Modifying a record means updating an already existing property, or adding a non-existent property, or removing an existing property, and how to do those are explained in the challenge.

1 Like

The question is worded poorly, I agree. Maybe breaking into a list would help:

  1. Your function needs to modify the collections object using data passed to your function. Your function then needs to return the entire collections object.
  2. The only data that will be passed to your function is id, prop, and value
  3. id is always the property you will modify in the collections object. The question calls this an albumn.
  4. prop is the name of the property you will modify within the album.
  5. value is the value that will be assigned to whatever prop refers to
  6. the property ‘tracks’ is a special case, as that is always an array
  7. There are some rules to account for, which follow
  8. if the prop variable is not equal to ‘tracks’ and the value isn’t an empty string, then set ‘prop’ equal to ‘value’ (think of this as your default action.)
  9. if prop IS ‘tracks’ then you’ve got two options to consider:
    9a. If your album (the id property of collections) has an empty ‘tracks’ property, then create an empty array for it, and push ‘value’ into it.
    9b. If your album already has a tracks property, then just push ‘value’ into it (unless value is an empty string, see below.)
  10. if ever your function is handed an empty string as ‘value’ then delete ‘prop’ from your album.

This seems like a lot of steps (and it is) but it isn’t a whole lot of lines of code. I think my solution was only 14 lines, and it could probably be tightened up even more.

Hope that helps!

10 Likes

I guess my question is, when I replace the values, what should I be seeing in the results box?

also it gives the example of a value as this

“and a value (like “Addicted to Love”) to modify the data in this collection.”

"Additcted to love is not anywhere in the collection. I am just utterly confused on this whole thing really. I think I can workout the code I need to get past the problem but am just super frustrated that none of it is making sense

1 Like

Ok so I just went step by step on the directions… still not overall sure what I am trying to do, which would be helpful, but this is what I came up with and it isn’t working. Can someone take a look? thanks!

if (prop !== “tracks” && value !== “”){
collection[id][prop] = value;
}
else if (prop === “tracks” && !collection[id].hasOwnProperty(“tracks”)){
collection[id][prop] = [];
}
else if(prop == “tracks” && value !== “”){
collection[id][prop].push(value);
}
else if(value === “”){
delete collection[id][prop];
}
return collection;
}

1 Like

You did great, just forgot one thing:

9a. If your album (the id property of collections) has an empty ‘tracks’ property, then created an empty array for it, and push ‘value’ into it.

You created a new array, but didn’t push value into it. When you add the missing piece the code will work.

3 Likes

Thanks so much for this explanation :slight_smile:

I solved the solution and this is what I came up with. My mindset for this was everything had to be solved and searched through the ID and PROP.

 function updateRecords(id, prop, value) {
    if(prop !== "tracks" && value !== ""){
    collection[id][prop] = value;

//I noticed some posts where they had an else if then an if for this one. This worked for me. Simple      enough.

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

 return collection;
}
6 Likes

Thank you so much for the detailed explanation. It helped me to unserstand the task! :slight_smile:

1 Like

This is what i passed with.

function updateRecords(id, prop, value) {

if (prop !== “tracks” && value !== “”){
collection[id][prop] = value;
} else if (prop == “tracks” && collection[id].hasOwnProperty(“tracks”) == false) {
collection[id][prop] = [];
collection[id][prop].push(value);
} else if (prop == “tracks” && value !== “”) {
collection[id][prop].push(value);
} else if (value == “”) {
delete collection[id][prop];
}

return collection;
}

does everything seem right?

1 Like

this is the right code
function updateRecords(id, prop, value) {
if (prop !== “tracks” && value !== “”){
collection[id][prop] = value;
}
else if (prop === “tracks” && !collection[id].hasOwnProperty(“tracks”)){
collection[id][prop] = [];
collection[id][prop].push(value);
}
else if(prop == “tracks” && value !== “”){
collection[id][prop].push(value);
}
else if(value === “”){
delete collection[id][prop];
}
return collection;
}