Record collection - one problem

Hi everybody :slightly_smiling_face:
I am new here. I started javascript section and I have a little problem with my code.
It is code for Record collection:

function updateRecords(id, prop, value) {
  if (prop !== "tracks" && value !== "") {
      collection[id][prop] = value;
  } else if (prop === "tracks" && collection[id][prop] !== "tracks"){
      collection[id][prop]=[value];
  } 
  if(value === ""){
      delete collection[id][prop];
  }
         
  return collection;
}

After run this code, I have the issue:

After updateRecords(2468, “tracks”, “Free”), tracks should have “1999” as the first element.

Maybe some of you see, what is wrong with my code?

Thank you for any suggestion and help!

Have a nice day!

collection[id]["tracks"] is or should be an array with strings inside, it will always not be equal to the string "tracks", as such you are always making collection[id]["tracks"] have value of [value] so if the array already had strings inside you are deleting them - if there are already values you should add new ones to the array instead

Try this:

<redacted>

There should have a condition to check whether the object has a “prop” property, the function is “hasOwnProperty()”.

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

If you want to compare your solution to others, use the Get a hint button on the challenge and there are alternative solutions you can compare yours to. Also, you can probably search older posts using the forum search feature or google the challenge name and find more there.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.

Sure, I don’t know this, thanks for your reminder.:grinning:

Hi guys,
thank you both for suggestions :slight_smile:
Now I am trying to change my code with using these tips.
Maybe it will work soon

Hi again!

This challenge was really hard for me. But I change my flow of thinking after I saw the hint for this problem.
Now I understand, that I must have a nested IF statement for ‘props’ equal ‘tracks’, then my code will be working good.
Now my code for ‘Collection record’ is like this:

function updateRecords(id, prop, value) {
  if(prop !== "tracks" && value !== "") {
      collection[id][prop] = value;
  } else if (prop === "tracks" && value !== "") {      
      if(collection[id][prop]){
          collection[id][prop].push(value);
      } else {
          collection[id][prop] = [value];
      }
  } else {
      delete collection[id][prop];
  }          
  return collection;
}

Maybe this code is not so well as can be, but I am happy that I’ve done this :slight_smile:
Thank you all for your help!