I am trying to solve the challenge Basic JavaScript: Record Collection but cannot find a solution. I would like to solve the challenge without peeking into solutions.
I would appreciate it if someone could tell me what am I doing wrong.
these are just a couple:
you never check the value of parameters prop and value
your return statements are varied, but you are asked to always return the collection object after the changes
I have now removed the parameters prop and value from the first if statement and
I have removed the return and left it only for the end to return the entire collection but immediately after the first if statement I am getting undefined. Why undefined. I mean if the challenge says
If prop isn’t "tracks" and value isn’t empty ( "" ), update or set the value for that record album’s property.
Shouldn’t I include collection[id][prop] to see if the the "tracks" is part of the prop
Below is now changed if statement which returns undefined
you are overwriting the whole sub-object, not just a variable
also remember that in an if/else if/else only one of the statements execute, so your return statement will almost never execute - maybe you want to put it after the whole chain?
Thank you very much @ilenia you have helped me a lot with this challenge and with the understanding of objects. I have made a new solution and I am not getting undefined anymore but I am still not passing all the requirements of the challenge.
I have placed my return outside of the statement but I do not understand the part when you say “you are overwriting the whole sub-object, not just a variable”.
My current code is below
function updateRecords(id, prop, value) {
if (prop !== "tracks" && value !== [""]){
collection[id][prop] = value;
} else if (prop === "tracks" || "tracks" === false) {
collection[id] = value;
} else if (prop === "tracks" && value !== "") {
collection[id][prop] = tracks.push(value);
} else if (value === "") {
delete collection.prop
}
return collection;
}
when prop is tracks one of the objects inside the collection object is overwritten. For updateRecord(1245, "tracks", "song title") it becomes like this
{
1245: "song title" // no more object
...
}
also, "tracks" === false will always give the same value as you are comparing a string and a boolean. Will always evaluate to false. Can be removed.
(there is other stuff below this, I have just mentioned the first couple of things)