Stuck on the Record Collection lesson

Stuck on the Record Collection lesson
0

#1

I don’t learn as well through written instruction, so please excuse me if the answer is right in front of me, sometimes I’ll miss something entirely too obvious.

With that said, I’m not sure where to even begin with this one.
Based off of the third sentence on the lesson ( If prop isn’t "tracks" and value isn’t empty ( "" ), update or set the value for that record album’s property.), I’ve written this out:

// Only change code below this line
function updateRecords(id, prop, value) {
  if(prop !== "tracks" && value !== ""){
    
  }
  return collection;
}

I think I’m mostly just missing how I get from A to B. I know that if someone puts in something new, it needs to append it to the collection object. I also know that if somebody puts in something that contradicts information that is already there, then it needs to simply change it.
I just don’t understand how I’m supposed to do that.


#2

I’ll go ahead and drop the entire problem here so that nobody has to go searching for it.

You are given a JSON object representing a part of your musical album collection. Each album has several properties and a unique id number as its key. Not all albums have complete information.

Write a function which takes an album’s id (like 2548 ), a property prop (like "artist" or "tracks" ), and a value (like "Addicted to Love" ) to modify the data in this collection.

If prop isn’t "tracks" and value isn’t empty ( "" ), update or set the value for that record album’s property.

Your function must always return the entire collection object.

There are several rules for handling incomplete data:

If prop is "tracks" but the album doesn’t have a "tracks" property, create an empty array before adding the new value to the album’s corresponding property.

If prop is "tracks" and value isn’t empty ( "" ), push the value onto the end of the album’s existing tracks array.

If value is empty ( "" ), delete the given prop property from the album.

Hints
Use bracket notation when link redacted.

Push is an array method you can read about on link redacted).

You may refer back to link redacted Introducing JavaScript Object Notation (JSON) for a refresher.


#3

You need to first create a written algorithm which is nothing more than a set of steps (logic) that you would use to return the correct value based on given function inputs (is, prop, value). You have outlined two steps, but you need to further describe what you mean by:

  • “I know that if someone puts in something new, it needs to append it to the collection object.”

  • “I also know that if somebody puts in something that contradicts information that is already there, then it needs to simply change it.”

With respect to the first bullet above, what do you mean append “it” to the collection object? Where in the instructions does it tell you to modify the collection object?

In the second bullet above, define how you would identify contradictory information that is already there and then what exactly would you change about it?

My main point of these questions is to get your to really think about what data you have and need to access to solve the problem and what you should be returning as a value. Read the challenge instructions again and identify your input and output to help you write a clear algorithm before you attempt to write any JavaScript code.


#4

So it wasn’t what I missed… it was a caveat of the problem that I assumed was there but actually isn’t.
I figured the ID could be a random variable, and if it wasn’t something that matched a value in the collection object then it would have to be added.

Now that I realize that isn’t the case this should be easier for me to figure out. I’m going to try my hand at it again. Also thank you for the algorithm tip, makes it much easier to visualize what I’m doing in the code instead of just blindly editing.

I’m sure I’ll be right back whether I get it wrong again or not, at very least to provide what I was missing for anyone else that gets hung up on this lesson in the future.


#6

I deleted my previous reply because I noticed a few things I’d done incorrectly.

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

#7

What if prop is “tracks”, value is not “”, but there already is a “tracks” property for the object? Your code always assigns an empty array and then pushes value regardless if the “tracks” property already exists.


#8

I see. I’ll continue looking at it. I’ve been stuck on this for far too long, getting pretty frustrating to be quite honest.


#9

Also, the code above is used in two places. It is not doing what you think it is doing. id is a string and it will not have any property other than length, so id[prop] will be undefined, thus collection[undefined] will also be undefined.

This means your delete collection[id[prop]]; will do nothing and your collection[id[prop]] = value; will do nothing.

I suggest you review the previous challenge on Accessing Nested Objects.


#10

So would the proper thing to do be
delete collection.id[prop]? because I kept getting undefined errors all yesterday no matter which way I tried to do this.

I just took a look at the lesson you linked me to and
var gloveBoxContents = myStorage.car.inside["glove box"]; was the answer for it, and maybe I’m incorrect but when I tried this yesterday in a very similar fashion it didn’t work. I’ll try it again like that and see if there was a typo or something like that.


#12

When you use dot notation, the object must have an actual property name with the same name after the dot. For example, does the collection object have an “id” property?

You should probably review the following challenge to clear up how to access properties with a variable.

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-variables