Hi evrybody!
First of all sorry for my English.
I’m struggling against a great confusion at a JS challenge and its sintaxis. This one specifically:
I wrote this and it didn’t work (obv).
function updateRecords(object, id, prop, value) {
if (prop != "tracks" && value != ""){
object.id.prop = value;
} else if (prop == "tracks" && !object.id.hasOwnProperty("tracks")){
object.id.prop = [value];
} else if ( prop == "tracks" && value != ""){
object.id.prop.push(value);
} else if (value == ""){
delete object.id.prop;
}
return object;
}
After a while i decided to take a look at the hints and I saw that the conditions and if statements were OK and the only difference was that I used dots to access to the objects instead of brackets.
I followed what the previous challenges taught me and I realy didn’t find any difference between trying to access to an object properties using . or …
So that’s why I open this topic. I have no problems with the logic of the challenge. My problem is that I cant understand why the function works only with brackets and not with dots as I did in previous challenges.
Thanks you all!
This is the solution:
function updateRecords(object, id, prop, value) {
if (prop !== 'tracks' && value !== "") {
object[id][prop] = value;
} else if (prop === "tracks" && !object[id].hasOwnProperty("tracks")) {
object[id][prop] = [value];
} else if (prop === "tracks" && value !== "") {
object[id][prop].push(value);
} else if (value === "") {
delete object[id][prop];
}
return object;
}