PABCon
January 10, 2020, 7:41pm
1
Hello All,
I want your help to understand why just one of my items doesnt pass.
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/record-collection
function updateRecords(id, prop, value) {
var check_prop = collection[id].hasOwnProperty(prop);
if(value === '')
{delete collection[id][prop] }
else {if(prop=="tracks") {
if (check_prop == true)
{collection[id].tracks.push(value)}
else {collection[id].tracks = [];
collection[id][prop] = value;
}}
else { collection[id][prop] = value}
return collection;
}
}
Your help would be great!
Hint: Your problem is here.
else {collection[id].tracks = [];
collection[id][prop] = value;
}}
What will the value of the tracks
property be after this code runs?
1 Like
PABCon
January 10, 2020, 7:52pm
3
well, I imagine it should be value.
1st line creates tracks and empty array
2nd line should say tracks = value (passed by user)
It is currently functionally identical to
else {
collection[id].tracks = value;
}
Can you see why that is wrong?
1 Like
PABCon
January 10, 2020, 8:00pm
5
because tracks does not exist, right?
Nope. That line will create tracks
.
What is the value of tracks
supposed to be? What type of value is it supposed to be?
PABCon
January 10, 2020, 8:04pm
7
its suppose to be a string passed by user.
Look at the instructions and the object model again.
Note that if tracks
exists you are pushing to it .
ilenia
January 10, 2020, 8:06pm
9
the property named tracks in the object, what type of value should it be?
ilenia
January 10, 2020, 8:07pm
11
and if you do collection[id].tracks = value
what type of value it becomes?
… and is value
an array? You’ve already said what it is, like six minutes back.
PABCon
January 10, 2020, 8:12pm
15
Yeah, i got what they said before. I had to switch it to array.
Amazing how a small detail makes the difference. wow, need to pay more attention.
Thank you all
Congratulations! Happy coding.
Generally, don’t post working solutions, as it takes away from others enjoying that AHA moment. If you do post a solution, for example for a code review, please blur it out.
PABCon
January 10, 2020, 8:14pm
18
Cheers Snowmonkey, will keep that in mind.
1 Like