Please explain this solution

here is the task:

  • Your function must always return the entire records object.
  • If value is an empty string, delete the given prop property from the album.
  • If prop isn’t tracks and value isn’t an empty string, assign the value to that album’s prop.
  • If prop is tracks and value isn’t an empty string, you need to update the album’s tracks array. First, if the album does not have a tracks property, assign it an empty array. Then add the value as the last item in the album’s tracks array.

Link: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/record-collection

here is the solution:

  if (value === ""){
  delete records[id][prop];
} else if (prop !== "tracks" && value !== "") {
  records[id][prop] = value;
} else if (prop === "tracks" && value !== "") {
  if (records[id].hasOwnProperty("tracks") === false) {
      records[id][prop] = [ ];
    }
    records[id][prop].push(value);
  
}

why did we put “id” and “hasOwnProperty” in the code? I don’t understand. I am super confused😵

1 Like

Having the solution to this challenge is pretty useless, to be honest. I would forget about this solution and try to write your own.

I tried but I didn’t know what to do

Then reading the solution won’t help.

Lets start with what about the problem you do and don’t understand. What do you understand about the problem?

well the function should return records and we already got that in the code, and we check if value is empty which is going to be if (value = "") and the we delete the prop which I think it’s going to be delete prop; and then we check if prop isn’t tracks with != and if value isn’t empty which is going to be else if ( prop != "tracks" && value != "") after that we assign value to that album’s prop which I don’t understand and I don’t know how to do…

That’s a great start! Can you fill in some lines of code to capture what you just said? You can leave comments for the bits you don’t have yet.

Something like

function updateRecords(records, id, prop, value) {
  if (value === "") {
    // Something with delete here?
  }
  // etc
  return records;
}
1 Like
if (value == "" ) {
  delete prop;
} else if (prop !== "tracks" && value !== "") {
  // assign value to prop which idk how to do
} else if (prop == "tracks" && value !== "" ) {
  // I need to update the tracks ...
}
// other code which idk how to do

  return records;
}

Ok, small thing here. Forget that == exists. You want ===.

Lets start here. You need to delete the property from the record. So you need to get at the record we’re talking about here. How do you get the record we want from the records object? (“I’m not sure” is a perfectly reasonable answer!)

2 Likes

well in the earlier challenge, it was this

ourPets[0].names[1];
ourPets[1].names[0];

So id is representing the album which is going to be those numbers “2468” , so is it going to be record[id].record[prop]

i don’t know, I’m lost

You aren’t too far off. Try it. record isn’t a variable, so you will run into trouble there. Also, why record[id].record?

You can always try console.log to see what happens.

to be honest I don’t know, it’s just what I thought of at that moment

Did you try console.logging it?

This solution is great!

However, I don’t think there is a need to include the condition && value !== "" in the else if statements since it must have been taken care of by the if statement.

Like this:

if (value === "") {
    delete records[id][prop];
  } else if (prop !== "tracks") {
    records[id][prop] = value;
  } else if (prop === "tracks") {
    if (records[id].hasOwnProperty(prop) === false){
      records[id][prop] = []
    }
    records[id][prop].push(value)
  }

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.