Basic JavaScript - Record Collection

Tell us what’s happening:
I got entirely too stuck on Record Collection, re-did the entire Javascript course up to Record Collection, and am still just as stuck as I was… I am really just at a loss.

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) {
  return records;
} if (value === "") {
    delete album[prop];
} else if (prop != tracks && value != "") {
    album[prop] = value;
} else {
    album[tracks] = album //this part is really just not computing
    album[tracks].push(value);
}

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

Challenge: Basic JavaScript - Record Collection

Link to the challenge:

Hello,
I have reviewed your code and I want to inform you that you have used the wrong syntax to access the property to delete a specific property.
You need to consider the arguments provided in the

function updateRecords(records, id, prop, value)

In place of “album” you just need to use “records” and then you have “id” which reference to the album id of the object which you have already in the editor.

The last thing I want you to practice on how to access a property of a nested object.

It is great that you solved the challenge, but instead of posting your working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Is this closer to what it should look like?

function updateRecords(records, id, prop, value) {
  return records;
} if (value === "") {
  delete id[prop];
  }

Leaving the return as the very first thing will immediately stop the function and make it do nothing.

You are accessing the property in a wrong way.

const myObject = {
     car1: {
        make: "Toyota",
        year: 2016
    },
    car2: {
       make: "KIA",
       year: 2015
}
}

const makerOfCar2 = myObject["car2"]["make"]

The variable makerOfCar2 will have a value of property make of the car2 object inside myObject.

And also you need return statement after condition has been performed.

Ok, thanks for your patience. Is this closer to being correct?

function updateRecords(records, id, prop, value) {
 if (value === "") {
  delete recordCollection[id][prop];
  }
  return records;

You should not directly reference the global variable recordCollection

Note: A copy of the recordCollection object is used for the tests. You should not directly modify the recordCollection object.

So would records[id][prop]; work?

Yup

1 Like

For the last part especially I’m really confused. Can someone show me where my code isn’t corresponding to the instructions? I’ll post the instructions and what I’ve got so far.

Inst:

  • If prop isn’t tracks and value isn’t an empty string, assign the value to that album’s prop.
  • 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.

Code:

} else if (prop != "tracks" && value != "") {
    value = records[id][prop]
  } else if (prop === "tracks" && value != "") {
    id.prop["tracks"] = [];
    id.prop["tracks"].push(value);
  }

This is backwards. You need to update records[id][prop] with the value

id is not an object, and it doesn’t have a property literally named prop. Also, this is saying that you always want to erase whatever is there and replace it with an empty array, but that’s not the instructions.

1 Like

Oh my god I passed the test thank you guys so much that was the worst two days of my life. :smiling_face_with_three_hearts: :slightly_smiling_face: :slightly_smiling_face: :upside_down_face: :melting_face: :innocent:

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