I believe you know this.
object(hasOwnProperty) can be true
or can be false
You were dealing with booleans in the curriculum, right?
I believe you know this.
object(hasOwnProperty) can be true
or can be false
You were dealing with booleans in the curriculum, right?
it sets the records property, if it isn’t tracks to whatever the value being added is.
So if the prop is ‘artist’ it will set the value to ‘ABBA’.
Yes, but I don’t know how to make the code do different things if something is true or false. This is wrong but I feel like I’m supposed to do something like this.
}
else if (records[id][prop].hasOwnProperty('tracks') && records[id][prop]['tracks'].hasOwnProperty = false) { // this condition will need tweaking
// this isn't how you set a property of an object
records[id][prop] = "tracks" + value + [];
}
Remember the difference between these three?
=
==
===
Well I do now and feel like a right plum pudding.
Assignment operator.
equality operator.
strict equality operator.
The tracks are just another property.
Sorry, but I do not know how to use that information.
And
You know how to set an object’s properties. The 'tracks'
array is another property. You set it in the same way, but you need it to be an array instead of a single value.
If it means something other than what I said before, I am totally lost as to what that syntax does.
it sets the records property, if it isn’t tracks to whatever the value being added is.
So if the prop is ‘artist’ it will set the value to ‘ABBA’.
I know how to make an array. I know how to set an objects properties. But I can’t work out how to combine those two things.
You are correct. This says ‘set records[id][prop]
to the value on the right side of the = sign’. If you want to set this equal to an array…
This is what I have now. I don’t know how else to set it equal to an empty array.
else if (records[id][prop].hasOwnProperty('tracks') && records[id][prop]['tracks'].hasOwnProperty == false) {
records[id][prop] = [];
}
to the right of an assignment operator can go anything - the way to assign an array to an object property is exactly what you did before, but instead than with a string (or string in a variable), with an array
That does set it equal to an empty array. What if you wanted an array that only held the value
?
Using push to push value to the array?
It’s not really necessary, you are creating an array in that moment, so you can create it how you want (empty, with values inside…)
Something like this?
else if (records[id][prop].hasOwnProperty('tracks') && records[id][prop]['tracks'].hasOwnProperty == false) {
records[id][prop] = [value];
}
Sure, that could work, or you can put it in there from the beginning… Either way.
What’s your full code now? You are close but still need some small fixes.
This is my current code. I think it’s the second to last else if that needs the fixes.
// Only change code below this line
function updateRecords(records, id, prop, value) {
if (prop !== "tracks" && value !== '') {
// correct
records[id][prop] = value;
}
else if (records[id][prop].hasOwnProperty('tracks') && records[id][prop]['tracks'].hasOwnProperty == false) {
// correct
records[id][prop] = [value];
}
else if (records[id][prop].hasOwnProperty('tracks') && value !== '') { // also small tweak needed here
// looks mostly good - small tweak needed
records[id][prop]['tracks'].push(value);
}
else if (value == '') {
// looks good
delete records[id][prop];
}
// correct
return records;
}
what’s the value of prop
here?