Basic JavaScript: Record Collection - cant find my error

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

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

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?

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.

the property named tracks in the object, what type of value should it be?

Oh you mean its an array

and if you do collection[id].tracks = value what type of value it becomes?

the same type as value.

… and is value an array? You’ve already said what it is, like six minutes back.

fixed now and works:

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 = [value];
    }}

 else { collection[id][prop] = value}

return collection;

}
}
1 Like

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.

Cheers Snowmonkey, will keep that in mind.

1 Like