Build a Record Collection - Build a Record Collection

Tell us what’s happening:

im more lost than a blind man in an empty room. I have tried everything i can think of and tried some of the other formats and codes ive seen on the forum. Im so confused with the whole object thing.

Your code so far

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'
  }
};

function updateRecords(records, id, prop, value){
  if(value=="") {
      delete records.id.prop
  } else if (prop!=="tracks" && value!=="") {
      records.id.prop=value
  } else if (prop=="tracks" && value!=="") {
      records.id.tracks=value
  } else if (prop=="tracks" && value!=="" && records.id.hasOwnProperty("tracks")==false) {
      let newProp=[]
      newProp.push(value)
  }
  return records
}

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36

Challenge Information:

Build a Record Collection - Build a Record Collection

let’s look at some visual cues to help you out first.
When you look at your code, do you notice that one of the parameters is a different color than the rest?

What do you think is going on there?

The grey color means the value is not being used.
You may now wonder: why? You may even think: I am using it!
But are you?

You may want to revise how to access object properties again.

should i use brackets like this?

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

it made the parameter change color so i assume thats right? but i still fail everything but 1

I wouldn’t get caught up on the number of tests you are passing or not. I would focus on the logic.

You used bracket notation for id. I like that. Its a good idea. Why is it important and does it have implications for other parts of your code?

1 Like

to be honest, the video said that dot notation and bracket notation both represent/call a property of the object. I thought bracket notation was used more for properties with spaces or unformatted names. But I think it serves as a call to both the value in the function and the object property? as in it considers them as the same? like it assumes the id parameters is in the contextt of the id property in the given array? I honestly don’t know I’m very confused

Lets try an experiment.

const myObject = {
  cat: "fred",
  dog: "sam",
}

const prop = "cat";

console.log(myObject.prop);
console.log(myObject[prop]);

What happens when you try this code?

ahh I see. the dot notation is undefined. I see they are different. How do they work? Would I need to change my "prop"s to brackets too?

by changing by "prop"s to brackets I got some tests to pass, I tried doing that with the “tracks” too, but that made them all fail again so I put it back to dot notation. this is what I have:

function updateRecords(records, id, prop, value){
  if(value=="") {
      delete records[id][prop]
  } else if (prop!=="tracks" && value!=="") {
      records[id][prop]=value
  } else if (prop=="tracks" && value!=="") {
      records[id].tracks=value
  } else if (prop=="tracks" && value!=="" && records[id].hasOwnProperty("tracks")==false) {
      let newProp=[]
      newProp.push(value)
  }
  return records
}

console.log(updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me"))

    1. After updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me"), tracks should have the string Take a Chance on Me as the last and only element.
  • Failed:5. After updateRecords(recordCollection, 1245, "tracks", "Addicted to Love"), tracks should have the string Addicted to Love as the last element.

  • Failed:6. After updateRecords(recordCollection, 2468, "tracks", "Free"), tracks should have the string 1999 as the first element.

the dot notation is used with the exact name of a property.
In Jeremy’s example what are the exact names of the properties given in the object?

(is there a property defined whose exact name is prop?)

1 Like

ohhh i understand, so its used to represent a property without using its actual name so that u can use a different variable (such as prop) to represent that property within a function? also, where else should i look for these remaining tests?

1 Like

Hmm, how is this updating the records object?

I honestly dont know how to even approach this… but this is what I got:

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

Slow down and look at my question.

How is that code UPDATING THE records OBJECT? Why would I be talking about the records object specifically? Do those two lines change the records object?

i feel dense lmao. I understand what I have to do, but maybe I am not familiar with the code needed. I tried this:

let newProp=[value]
      recordsObject.push(newProp)

is there a different method i should be using? other than push?

You are supposed to update the records. You do not use the variable records at all in those two lines, so you probably are not changing the records.

What change are you trying to make to the records with this part of the code? What should the final object look like after you correctly execute the requested logic?

how am i not using records? how do i call records? im trying to add a new property to an object in the records array. should i call the object by the id?

it’s not that Jeremy was saying you were not ‘using’ records.

You’re obviously using them for example here:

This is where you delete something from records. So you do “use” it.

Jeremy was trying to give you a hint.
So let’s see what his hint was leading towards?

First, what does this exercise want us to do “If prop is tracks and value isn’t an empty string”?

and if you can answer this one correctly, then the next question to ask is:
Which line of code that you have written handles that?

Where are you using records in these two lines we were talking about? I didn’t see it?

You don’t call objects. You call functions. records is an object.

Yes, this is the important part. records is the array of objects. So it should be part of the code you write to do this.

Here you’re adding a property to a record in the records object.

ahh I see. I had to use the id of the object to refer to the object in the array, and then add a prop. I don’t understand how that makes a new prop tho, wouldn’t that just be referencing an existing property such as artist?