Record Collection - no idea where to start

Okay I have this now and it isn’t using a string for hasOwnProperty.

I thought it represented the record, record Id and the prop data.

I’ve been trying to access prop to make this work for the past 4 days. Apparently that is the key to everything but I cannot work out how to do it. It is very frustrating that I cannot grasp this.

// Only change code below this line

function updateRecords(records, id, prop, value) {

if (prop != "tracks" && value != "") {

records.prop = value;

}

else if (prop == "tracks" && records.hasOwnProperty("tracks")){

tracks.push([] + value);

}

else{

return records;

}

}

updateRecords(recordCollection, 5439, 'artist', 'ABBA');

Closer, but this won’t work either. Does the object holding all of records ever have the "tracks" property?

This challenge is 100% about understanding what records, id, prop, and value mean.

What is records?

What is records[id]?

What is records[id][prop]?

How about you do this?
Try to understand what these mean:

  1. records,
  2. id,
  3. prop, and
  4. value

in comparison to the passed arguments which are

  1. records === recordCollection (can’t change except if it’s another object which is intended to be passed as argument)
  2. id === 5439 (it can be 5440, 5441…)
  3. prop === artist (can be ‘tracks’…)
  4. value === ‘ABBA’ (the value of prop).

If it’s understood to this point, then, the focus should be on accessing it, then.

Records - I don’t know. I was just using it because it seemed to be important. I can’t see it anywhere in the earlier Javascript and it seems to just exist as part of update records, I don’t know what it means or dose.

id = those numbers identifying the individual records.

Prop= artist, tracks, albumTitle.

Value - just got told it’s the string attached to prop.

I have this now but it’s still wrong.

function updateRecords(records, id, prop, value) {
if (id.prop != tracks && value != "") {
id.prop = value;
}
else if (id.prop == tracks && records.hasOwnProperty(tracks)){

id.push(tracks + []);
}
else{
return records;
}
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');

Oh I didn’t know that was what value meant? I was looking for a numerical value or something. That explains a lot that I was confused about.

I have this now. I am still not doing it right despite all the help. :confounded:

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

id.push("tracks :" + []);
}
else{
return records;
}
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');

This challenge is always a challenge for those, who are starting out and are new to this. I was there once and have made it through. So, I hope you would too.
All the best.

2 Likes

You should start everything again from the beginning. Make sure each concept is understood, and you’ll be good.

I understand what you explained, which concepts do I need to understand?

(Not trying to be difficult or anything I honestly don’t know what concept/understanding I’ve been missing for the past 5 days.)

The dot notation still won’t work, lol

This is the key piece of syntax. The challenge is all about understanding this one piece of syntax. If you don’t know what it does, add a console.log to your function to see.

1 Like

Your understanding isn’t inadequate, just that this challenge requires you to construct logic in a way you’ve not been familiarized with in the previous lessons.

Okay I’ll change it back. The only reason I keep changing the notation is because I’m still getting X’s for every single lesson objective so assumed I still must be getting the notation wrong.

Where do I put the console.log in my function?

What have you tried. Put it a few places and see what it shows.

Wherever I put it that doesn’t just cause a syntax error with the red wriggly lines I get.

‘ReferenceError: Can’t find variable: tracks’

Meaning the tracks variable doesn’t exist. Which I don’t understand because it must exist. It’s up there in the recordsCollection code and is assigned strings. Is my code making tracks stop existing somehow?

tracks is not a variable though? Only records, id, prop and value are your variables.

What code are you using?

Let’s start with this:

records i.e the main object (recordCollection)

(id) this stands for the identifier. It could be anything but they are numbers, here. You can access using bracket notations. Like this: records[id]
which is the same as writing recordCollection[5140]

It will display the targeted id but since we ain’t targeting only one id, it’d be better to use the initial method of accessing it.

(prop) these are the properties in the id:
such as tracks, albums, e.t.c. it can be accessed using dot notation or bracket notation.

recordCollection[50].artist
recordCollection[50][‘artist’]

Instead of accessing directly, access using the holder defined in the function this way:
records[id][prop]

is it understood to this point?

1 Like

Okay. So I think I was mixed up because track is a property after checking previous lessons.

So okay those are variables and those are properties. But how do I access tracks without confusing the code into thinking I’m after a variable called tracks?

Here’s my code.


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

records[id][prop].push(tracks + []);
}
else{
return records;
console.log;
}
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');

You should check the object you’re accessing, you will find tracks there as a property.

Back way, way up. This challenge is impossible if you don’t understand the structure of the object… Do you know what this expression does yet

The challenge is literally impossible without understanding this one piece of syntax.

I hope this makes sense but this is my understanding

Records → id → prop

I understand it like a computer directory in my head. I’m using this directory (the bracket notation) to access the properties I need to access.

This is how I understand Javascript objects also, as a file structure. I know they’re not exactly like that, but it’s how I get my head around them and the idea of accessing data in them.

Right, so in order to update the records, you need to access records. If you want to update the idth record, then you need records[id].

So lets handle things one at a time

  • Your function must always return the entire record collection object.
  • If prop isn’t tracks and value isn’t an empty string, update or set that album’s prop to value.
  • If prop is tracks but the album doesn’t have a tracks property, create an empty array and add value to it.
  • If prop is tracks and value isn’t an empty string, add value to the end of the album’s existing tracks array.
  • If value is an empty string, delete the given prop property from the album.

Starting with this code here, how do you accomplish the first bullet point? Don’t worry about the rest. How do you do the first one?

// Only change code below this line
function updateRecords(records, id, prop, value) {
  console.log("id:", id);
  console.log("prop:", prop);
  console.log("value:", value);
  console.log("records[id][prop]:", records[id][prop]);
}