Basic JavaScript - Record Collection

Tell us what’s happening:
I am not sure why my code is not working and I want to figure it out without looking at the solution. Please advise.

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

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

updateRecords(recordCollection, 5439, 'artist', 'ABBA');
type or paste code here
// 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
let tracks = [];
function updateRecords(records, id, prop, value) {
  if (value == " "){
    delete updateRecords.prop;
  } else if (prop != tracks && value != ""){
    value = prop;
  } else if(prop == tracks && value != ""){
    recordCollection.push(tracks);
  }
  return records;
}

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/114.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Record Collection

Link to the challenge:

What does this line do?

What does this line do?

What does this line do?

What is this variable outside of the function?

Hi Jeremy, Thanks for replying.

delete updateRecords.prop

This line should delete the prop parameter but, after thinking about it should it be delete recordCollection.prop ?

value = prop; should assign the prop to the value parameter to fulfill the following requirement. But after closer inspection, I realize the instructions are asking for the opposite…In other words, the value should be assigned to prop. ie prop = value

recordCollection.push(tracks) is my attempt to fulfill the following requirements. If prop is tracks and value isn’t an empty string, you need to update the album’s tracks array. First, if the album does not have a tracks property, assign it an empty array. Then add the value as the last item in the album’s tracks array.
However, maybe I should update to tracks.push(value)?

let tracks = [] is to declare a tracks variable and assign it an empty array.

That’s not quite what that line does. It deletes the parameter called prop, not the parameter whose name is given by the variable prop.

You should not use the global variable at all. You should only use the 4 function arguments.


You don’t want either of these. This function should update the records object, but you are not using records anywhere on this line.


This is not updating the specific record given by the id number. Also, you should never access the global object.


But how is doing that out in the global variable space helping you?

I’ve been missing in action for a couple of weeks but looking to finally figure this out.

That’s not quite what that line does. It deletes the parameter called prop, not the parameter whose name is given by the variable prop.
You should not use the global variable at all. You should only use the 4 function arguments.

I’m not sure what to do in that case. Would delete id.prop; work or maybe id.pop(prop); or maybe record.id.pop(prop)?

Hmm, in that case maybe records = value;?

So maybe instead something like let tracks= []; id.tracks.push(value);

I think you are fundamentally missing the boat on what all these arguments are.

function updateRecords(records, id, prop, value) {
  // These are the only 4 variables you can use
  console.log("records:", records);
  console.log("id:", id);
  console.log("prop");
  console.log("value");
}

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

Some questions to get you started:

  1. How do you access a property of an object?
    https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-bracket-notation

  2. How do you access a property of a nested object?
    https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/accessing-nested-objects

  3. How do you change the value associated with a property of an object?
    https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/updating-object-properties

  4. How do you delete a property from an object?
    https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/delete-properties-from-a-javascript-object

Here’s my record collection:

const recordCollection = {
  123: {
    artist: "Frogmen",
    album: "Ribbit Rumble",
  },
  456: {
    artist: "Catz",
    album: "Just Barking",
  }
}

And here’s my function call:

updateRecords(recordCollection, 456, 'album', 'Just Meowing');

Let’s break down what the goal is. I’ve given you my record collection. I’ve given you an id (456) which specifies a single record in the collection.

{
  123: {
    artist: "Frogmen",
    album: "Ribbit Rumble",
  },
  // `id` is 456 so we're talking about this record ↓
  456: {
    artist: "Catz",
    album: "Just Barking",
  }
}

I’ve given you a property (“album”) on the record.

{
  artist: "Catz",
  // `prop` is "album" so we're talking about this property ↓
  album: "Just Barking",
}

And lastly I’ve given you a value (“Just Meowing”) to update that property to.

{
  artist: "Catz",
  // change this property to the `value` ↓
  album: "Just Meowing",
}

In the end I expect you to return the updated record collection, which should now look like this.

{
  123: {
    artist: "Frogmen",
    album: "Ribbit Rumble",
  },
  456: {
    artist: "Catz",
    // we've updated record 456's album property
    album: "Just Meowing",
  }
}

Beyond that there are the special requirements for the tracks property as well as deletion, but the above is the basic case, so I suggest you tackle it first.

Thanks for chiming in. I understand what you are saying. Now my questions are the following:

  1. Do I need to use an if/else statement?
  2. Should I simply use the arguments to update the records? I guess more specifically I should use dot notation with the arguments. Ex. records.id.prop = value;

You will need conditional logic, yes.

You cannot use dot notation when property names are stored in variables. You have to use bracket notation.

Using the console log was helpful. Thanks. My questions are the following:

  1. Do I need to use an if/else statement?
  2. Should I simply use the arguments to update the records? I guess more specifically I should use dot notation with the arguments. Ex. records.id.prop = value;

You will need conditional logic, yes.

You cannot use dot notation when property names are stored in variables. You have to use bracket notation.

Yes, you need to use the other arguments to update the records object.

Trying is free. You should try things out. I’ll show how how to try things out.

Let’s try using dot notation.

// setting up the initial record collection
const recordCollection = {
  123: {
    album: "Good Album",
  }
}

function updateRecords(records, id, prop, value) {
  // setting the value via dot notation
  records.id.prop = value;
  return records;
}

// calling the function
updateRecords(recordCollection, 123, "album", "Bad Album");

// checking the changes to recordCollection
console.log(recordCollection);

Okay, we’ve passed in the record collection, and we’re telling it to update 123’s album property to “Bad Album”.

But we get an error:

Uncaught TypeError: records.id is undefined

I guess we can’t use dot notation here. It turns out that the record collection doesn’t have a property with a key named “id”. So trying to set a property “prop” on it throws an error.

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