Hi campers,
today I feel me really frustrated cause I feel I haven´t learned anything!
When I muss to solve individualls challenges, I find they quit easy, but then, comes the “CHALENGES”.
It is normal that difficulty grow up, but I feel like I climbing little sand obstacles, and sudenly the EVEREST is in front off me and I muss climbing 8.000 meters!
I tried to solve the challenge for my self, but my best try was this code:
// Only change code below this line
function updateRecords(object, id, prop, value) {
if (value == "") {
delete object.prop;
}
else if (prop == "tracks" && value != "") {
tracks.push(value);
}
else if (prop != "tracks" && value != "") {
prop = value;
}
else if (object.hasOwnProperty("tracks")) {
tracks = [value];
} else
object.tracks = [value];
return object;
}
Obviously I didn´t pass the challenge. After that failure, I search for help in “Get hint”, and I read the solutions. I still not understanding it at all:
// Only change code below this line
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;
}
I´ve compared both, and I found that all my if´s statements were more or less right, but the rest…
1 - I don´t understand why object[id][prop]
must be used each time.
2 - Notation && !object[id].hasOwnProperty("tracks")
was never before used during JavaScript lessons (never)
3 - Why notation delete object[id][prop]
has no point ( . )
as it was showed during lessons?
Please if someone has time to help it will be really helpfull from you.
Thanks and have a nice weekend.