I check the Hint section, there are plenty of answers there, very old. They, however, gave me guidance on what I needed to do. Of my logic is flawed please let me know.
If value is an empty string, delete the given prop property from the album:
if (value === “”) {
delete collection[id][prop];*
}*
** If prop isn’t tracks and value isn’t an empty string, update or set that album’s prop to value .**
if (collection[id][prop] !== “tracks” && value !== “” ) {
value = collection[id][prop]
}
** If prop is tracks but the album doesn’t have a tracks property, create an empty array and add value to it.** (I’m struggling with this one)
If prop is tracks and value isn’t an empty string, add value to the end of the album’s existing tracks array.
if (collection[id][prop] === “tracks” && value !== “”) {
collection[id][prop].push(value)
}
return object;
I’m totally lost. FCC answer is completely different, although logical too. I even copied and pasted all other answers given by people in the Hint section, some others I found on google and none of them worked and I mean NONE of them worked!
What’s happening? Find it hard to believe there’s only one way to solve the problem.
The challenge was changed at one point so you have to make the adjustments needed (look at the function parameters). There are plenty of answers in the thread that works just fine after making the adjustment.
BTW, object[id][prop] === 'track' is not the same as prop === 'track' (or !== same thing).
I probably don’t mate. Understand you’re trying to help by giving me hints, but really super stuck here. Also, how can I log out object[id][prop] at the top? Because one of the hints FCC bot gives you is to access the object in that way?
If prop isn’t tracks and value isn’t an empty string, update or set that album’s prop to value .
Is working with this
if (object[id][prop] !== "tracks" && value !== "") {
object[id][prop] = value;
}
If prop is tracks but the album doesn’t have a tracks property, create an empty array and add value to it.
Just put console.log(object[id][prop]) at the top of the function.
prop is a parameter, its value is just a string passed to the function (“artist”, “tracks”, “albumTitle”). You are looking at the object property object[id][prop].
For example for the function call:
updateRecords(collection, 2468, "tracks", "Free")
The value of object[id][prop] will be [ '1999', 'Little Red Corvette' ]
Hey mate, you were very cryptic for how confused I was. At least I thought that. I can see now I was heading in the right direction -I believe- and you were just pushing bit by bit. That last answer helped, I was calling the total value of the prop on my If Conditional instead of the parameter whose value was changing.
Got really stuck on the array one. Just a quick question.
That’s my confusion, why? Isn’t .push() supposed to add value to the newly created array and I need to put the parameter in the parentheses? Or is it a default setting when creating a new array because I’m already declaring prop===“tracks” as value in my operator?
This is my solution that worked:
if (prop !== "tracks" && value !== "") {
object[id][prop] = value;
}
if (prop === "tracks" && !object[id].hasOwnProperty(prop)) {
object[id][prop] = [];
}
if (prop === "tracks" && value !== "") {
object[id][prop].push(value);
}
if (value === "") {
delete object[id][prop];
}
return object;
}