I’ve been working on this problem for a day and a half now.
My question is:
collection[id][prop]=[value]; // What is this doing?
and why is the above different from:
collection[id][prop] = value;
That is what’s confusing me.
Thanks!
I’ve been working on this problem for a day and a half now.
My question is:
collection[id][prop]=[value]; // What is this doing?
and why is the above different from:
collection[id][prop] = value;
That is what’s confusing me.
Thanks!
The first one puts the value in an array, the second is not.
So if the value was 1
, then the first one is [1]
, the second one is 1
Thank you Dan. So to further clarify you are saying to put a value where one does not exist in a collection object, the syntax is:
collectionObject[someId][someProp] = [someValue];
What about
collectionObject[someId][someProp] = someValue;
What is the logic to that?
to rephrase…
function updateRecords(id, prop, value) {
if (prop === "tracks" && value !== "") { // if prop === tracks and value is not empty
if(collection[id][prop]) { // if id and prop exist
collection[id][prop].push(value); // push the value (in this case ABBA the artist to the end of the tracks array.
}
else {
collection[id][prop]=[value]; // Other wise???? What is [value] doing, why not just value why the brackets?
}
} else if (value !== "") { // if value is not empty (if artist argument is provided when the function is called)
collection[id][prop] = value; // ??? why is this not in brackets? This is where my confusion is.
} else {
delete collection[id][prop];
}
return collection;
}
tracks
is supposed to be array of tracks. [value]
isn’t special syntax, it’s just a value in an array, like [1,2,3,4]
or ['hello']
. Because tracks
is an array, if it doesn’t exist on the object you need to create it. Just value
doesn’t work, because that’s not an array of things.
I reread the original problem.
= [values] is creating an empty array before
Ok so [value] is creating a new array but not putting anything in that array, right?
a value is being inserted at this line?
collection[id][prop] = value;
So say you have functions that looks like:
function example1(value) {
return value;
}
function example2(value) {
return [value];
}
So if I execute example1('hi')
, it returns 'hi'
. If I execute example2('hi')
, it returns ['hi']
.
No. As I say this isn’t some special syntax. This is an array with one element, the element is whatever the variable value
is:
[value]
This is an empty array:
[]
This is an array with five elements in, all with whatever the value of the variable value
is:
[value, value, value, value, value]
In the challenge, the property tracks
should be an array: it is a list of tracks for a particular album.
If you use the function to add a track to an album, but there is not a tracks
property already, then you need to set it to be an array, and to put that track in the array