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?
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.
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!
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.
The question is worded poorly, I agree. Maybe breaking into a list would help:
- Your function needs to modify the collections object using data passed to your function. Your function then needs to return the entire collections object.
- The only data that will be passed to your function is id, prop, and value
- id is always the property you will modify in the collections object. The question calls this an albumn.
- prop is the name of the property you will modify within the album.
- value is the value that will be assigned to whatever prop refers to
- the property ātracksā is a special case, as that is always an array
- There are some rules to account for, which follow
- 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.)
- 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.) - 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!
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
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;
}
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.
Thanks so much for this explanation
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;
}
Thank you so much for the detailed explanation. It helped me to unserstand the task!
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?
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;
}