Basic JavaScript - Record Collection: Seeking a deeper understanding

Tell us what’s happening:
Describe your issue in detail here.
I’ve been on this exercise for a few days and I’m stumped. I have read the forums and I understand this is an object with other objects and arrays nested inside it. I looked at the hints page and understand that records[id][prop] is being used to access a value of a property such as artists, tracks, etc, within the object of records. I feel like i’ve maybe done something very wrong or i’m not understanding some fundamental concept or maybe i’m over looking something. But i feel like I followed the directions exactly, also on the Free Code Camp youtube theres a video going over this but the instructor works backwards starting with the delete property function. I’m not sure if it’s because his code is a little different or maybe the instructions were different as that video is three years old, but theres also more than one way to do this as i’ve seen other people’s examples and they don’t look like that. But i seek a deeper understanding of why my code isn’t working. Thank you for the assistance.
Your code so far

// Setup
const recordCollection = {
  2548: {
    albumTitle: 'Slippery When Wet',
    artist: 'Bon Jovi',
    tracks: ['Let It Rock', 'You Give Love a Bad Name']
  },
  2468: {
    albumTitle: '1999',
    artist: 'Prince',
    tracks: ['1999', 'Little Red Corvette']
  },
  1245: {
    artist: 'Robert Palmer',
    tracks: []
  },
  5439: {
    albumTitle: 'ABBA Gold'
  }
};

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

  } else if (value === "") {
  delete records[id][prop];
}
return records; (edited)
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Record Collection

Link to the challenge:

Have you tried to add some console.log statements to through your code?

This would help highlight the execution.
Then you can call your function with one of the failing test cases to see what is happening specifically in that case.

I’m afraid i don’t understand what you mean. Where would I put the console.log? I did see with another user on the forum that they were to told to run the console.log to sort of figure out what everything was such as this:

function updateRecords(records, id, prop, value) {
console.log(“records is”, records);
console.log(“id is”, id);
console.log(“prop is”, prop);
console.log(“value is”, value);
console.log(“records[id] is”, records[id]);
console.log(“records[id][prop] is”, records[id][prop]);

is that sort of what you mean? I have no idea where i’d even put that though or how that would factor into a function

console.log helps you log variables so that you can check what is actually happening vs your own understanding of your code.

For eg.

In the above if statement, what do you think the value of prop will be after it executes the code?

I set it as value, and I think it will be value right? Unless i’ve written that wrong as well but how would console log that? would that be like console.log(prop)?

Yes. you could log the prop.

You could log other things too.

Then change the call at the end to be something that is failing and that you believe should be working.

For eg this is what you have now

I believe this is supposed to find a record of id 5439 and add the artist property and value to it.
So you can leave this and check if the records are being updated or not using the log.

Add as many logs as you like to understand the state of the variables as you go through the code.

Some other things you may want to know:

=
Is not the same as
===

And
The records object needs to be used to store (not just delete) values.

And
You should never modify an input parameter unless requested to. In this case the only input you should be modifying is records.

So i tried console.log(updateRecords) and it gave me [Function: updateRecords], i’m not sure what that’s supposed to mean? I don’t really get what you’re saying about changing the function call, there’s only one function. and how would i know if something is or isn’t being updated, i don’t get that either. Also i don’t think i put = in anything, i always used either === or == and did i modify the input? I don’t think i did but i’m just putting in what the exercise told me. I’m very confused i’m sorry :sweat_smile: also where does records[id][prop] come into this? I saw it being used in some other forum posts but i don’t get why you’d do records[id][prop] = value rather than what i have which is return prop===value.

okay,
maybe it is good to go back and revise some things.

Like what is = and what is ===.

Here:

and

and

ok will do! thank you.

So i took a look at that those exercises again and i think i get it. it’s not (prop !== “tracks” && value !== ‘’){
prop===value

it should be (prop !== “tracks” && value !== ‘’){
prop=value bc i’m assigning the value of value to prop right?

yes and no
= is the assignment operator
but prop = value is still wrong.

What is prop do you think? Does it have anything to do with the records you are trying to update?

so like in the hint page it talks about records[id][prop], would that be more appropriate. Thats kind of what i was confused about at first but then i realized it’s saying that records us the object and you’re accessing the property of id and the value of prop associated with that id? Is that correct? I went back and reviewed the object lessons and that was my take away.

Props is the input parameter. It tells you the value of the property being accessed within a single record object. So you should not modify it.

Records on the other hand is the object containing the records you are accessing. So yes, you can use bracket notation to get at each record using its id and to get at each record’s properties.

But it says to assign the value to prop so in that case wouldn’t that be ok? If not where does records[id][prop] fit into this? The last bullet point on the instructs says " * If value is an empty string, delete the given prop property from the album." so wouldn’t you be modifying the prop value there by deleting it? in the video going over this they write delete collection[id][prop]. I know that that’s an older example and here it’s records instead of collection but i think it’s the same principle right?

prop is not the same as records[id][prop]

prop is the value you get from whomever calls the function

records[id][prop] is a reference to a value stored in an object inside another object.

Here is an analogy.
When you have a bank card and it has your name and bank account number on it. Is that card your account or just something that helps you access the account?

You actual bank account is where you do adding of money and removing of money. The bank card is never modified. It just tells us your name and account details so we can find it.

Id and prop is like that bank card. It just tells us something we should be looking for but it is not something we can modify.

We use prop to find the real prop we want to modify or delete. We also use prop as a value when we are adding a new property.

All the changes are happening inside records not inside prop.

Ok i finally got it, it took messing around with this for a while and looking at the video but i passed and i think i get why
function updateRecords(records, id, prop, value) {
if (prop !== “tracks” && value !== ‘’){
records[id][prop]=value
} else if (prop === “tracks” && records[id].hasOwnProperty(“tracks”) === false) {
records[id][prop] = [value];
} else if (prop === “tracks” && value !== ‘’) {
records[id][prop].push(value)
} else if (value === “”) {
delete records[id][prop];
}
return records; (edited)
}
updateRecords(recordCollection, 5439, ‘artist’, ‘ABBA’);
console.log

after you explained the records[id][prop] thing i understood why i needed to use that for most of the returns, but what what was still tripping me up was " * If prop is tracks but the album doesn’t have a tracks property, create an empty array and add value to it." I had competely written that wrong like this
} else if (prop === “tracks” && records !== records.hasOwnProperty(tracks)) {
return value +
so then i changed it to
} else if (prop === “tracks” && records.[id]hasOwnProperty(tracks) === false) {
records[id][prop] + [value]

because what that is saying is if prop has tracks but the album (the id) in the object doesn’t have a track property you need to create that track array with a in it value right?

do you have a typo here? Was it supposed to say:

records[id][prop] = [value];

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