What a Record Collection Challenge!

Hi campers,

today I feel me really frustrated cause I feel I haven´t learned anything!

When I muss to solve individualls challenges, I find they quit easy, but then, comes the “CHALENGES”.

It is normal that difficulty grow up, but I feel like I climbing little sand obstacles, and sudenly the EVEREST is in front off me and I muss climbing 8.000 meters!

I tried to solve the challenge for my self, but my best try was this code:

// Only change code below this line
function updateRecords(object, id, prop, value) {

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

Obviously I didn´t pass the challenge. After that failure, I search for help in “Get hint”, and I read the solutions. I still not understanding it at all:

// Only change code below this line
function updateRecords(object, id, prop, value) {
  if (prop !== 'tracks' && value !== "") {
    object[id][prop] = value;
  } else if (prop === "tracks" && !object[id].hasOwnProperty("tracks")) {
    object[id][prop] = [value];
  } else if (prop === "tracks" && value !== "") {
    object[id][prop].push(value);
  } else if (value === "") {
    delete object[id][prop];
  }
  return object;
}

I´ve compared both, and I found that all my if´s statements were more or less right, but the rest…

1 - I don´t understand why object[id][prop] must be used each time.
2 - Notation && !object[id].hasOwnProperty("tracks") was never before used during JavaScript lessons (never)
3 - Why notation delete object[id][prop] has no point ( . ) as it was showed during lessons?

Please if someone has time to help it will be really helpfull from you.

Thanks and have a nice weekend.

Hi,

  1. You need to specify, to which key you want to add provided value - so in this case, you want to add value to object which has the specified id as key name (you find this object with object[id]) and then with [prop], you add provided property to the object (or modify it).
  2. The exclamation mark (!) in front of a variable tells javascript to use opposite value of it (for example: true is same as !false).
  3. You can use either object.keyName or object[“keyName”] - in both cases you get the value of value of specified key inside an object. In this case, you have variables that represent keys so you must use bracket notation (obj[keyVariable]).

I hope that this helped you. I felt the same, when I was trying to learn javascript. I suggest you to check out https://www.w3schools.com if you don’t understand, what some part of the code does.

1 Like

Hello gbucar,

thanks for your support. I finally understand why backets were used. Thank you to explain the concept true and !flase, it doesn´t appear during the challenges.

I ´ll use your recomended wev site to consult some doubts.

Again, appreciated.

I’m going to throw in some freeCodeCamp lessons for context/reference. Referencing past lessons or MDN can be super helpful.


The property for delete must be accessed with bracket notation because the property we want is stored in a variable. This is discussed here

This same bracket notation is needed because the object contains all of the records, with their properties, listed by id.

The logical and was introduced here:

The idea of falsyness can be seen here:

though you can just use a != true and it looks like you know the != syntax pretty well.


It looks like you moved a bit too fast through this question and didn’t quite understand the data structure fully. We here on the forum can help clarify the problem if you ask.

I think that if you had posted your partial code, we could have coached you through tweaking it so I’d would pass. That way you wouldn’t have needed to copy the solution.

Coding is a marathon, so slower is better, and slower will help you learn faster. Please ask us questions - we volunteer here because we like helping. This stuff is hard to learn!


I’m a pretty big fan of JavaScript | MDN
as a reference. I think it’s more comprehensive.

2 Likes

JeremyLT, my respect!

Now I understand why you´re called Dungeon Master…

You´ve 100% reasson. The best sentence was “Coding is a marathon, so slower is better, and slower will help you learn faster.” It clicked me something!

I´m in a hurry situation cause my personal background. My time is limited (until July), and until those date, I muss learn everything I can. I know, it sounds crazy, but it is.

I´ll try to get things slowly and not to be so anxious to understan it. Languaje is another barrier for me here.

So, Jeremy, I really appreciate your post. Thank you.

1 Like