Build a Record Collection - Build a Record Collection

Tell us what’s happening:

Hi there, first of all, please spare your judgement, I’m completely new and just trying to understand what’s happening here:

Question - Is this the part of the code that adds a new property to the object if the property doesn’t yet exist?

else if (prop !== “tracks” && value !== “”) {records[id][prop] = value;

Here’s my complete code (after the predefined array) for reference:

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

Did you write that code to add a new property? The code should match your intent when writing it.

Yes. I just updated my message to include the full code in the Lab.

I followed the user stories and eventually passed. But I just want to make sure I understood it correctly. I spent a few hours on it and may have gotten myself confused

If you wrote it to add a new property and it seems to pass the related tests, then that’s probably what it does.

How could we check to see if that line is actually doing that? Is there a function call you could run to see?

Hmm. That’s a good question. I ran a few console logs to add different ‘test’ properties, and they all just got added, so I think this answers the question.

I guess I thought I needed some sort of “operator” to add properties, but I guess we don’t.

Thanks you for helping me through think this through