Basic JavaScript - Record Collection

Tell us what’s happening:
I’m having trouble figuring out why it’s not passing two of the tests.

The third test passed, but the second from last test did not. they appear very similar aside from either tracks not being set or artist not being set. My else if statement doesn’t specify between the two, so shouldn’t it work?

And I have no idea why the second test isn’t passing.

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 != "") {
    records[id][prop] = value;
    // If property is not equal to "tracks" and value is not undefined, set property to value

  } else if (prop == "tracks" && !id.hasOwnProperty("tracks")) {
    const tracks = [];
    records[id][prop].push(value);
    // If property is equal to "tracks" and id does not have property "tracks" create tracks array and add value
    
  } else if (prop == "tracks" && value != "") {
    records[id][prop].push(value);
    // If property is equal to "tracks" and value is not undefined, add value to "tracks"

  } else if (value === "") {
    delete records[id][prop];
    // If value is an empty string, delete prop

  }
  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/109.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Record Collection

Link to the challenge:

What does this line mean?

It’s checking the id for the property “tracks”. But since the test asks to check if the property does not include tracks, I added the not operator beforehand. So instead of checking if it has that property, it would be checking if it does not have that property.

But id is a string. It will never have that property

Hi! I have just run your code, with a few changes, and the tests passed accordingly.

The issue is present in:

!id.hasOwnProperty("tracks")

Do recall that id is a string. Therefore, the hasOwnProperty doesn’t exist. You would like to check if the record (an object, inside the records object) with the specified id has a track property. Once you manage to access the object itself, the function you chose to use will be available.

Lastly, when it comes to…

    const tracks = [];
    records[id][prop].push(value);

You are creating a local variable that is never used (tracks), and the push function is being called for an object property that doesn’t yet exist. In such scenario, you might just want to declare it an array with the value inside of it.
It will also work if you choose to declare it as an empty array and then run the push function, if you prefer to use it.

I hope to have helped! If you have any doubts, do let me know.
Cheers :slight_smile:

It is great that you solved the challenge, but instead of posting your full 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.

Right, thank you for the feedback! It’s my first day in the forum, and I had not thought of such factor. I’ll edit my answer to correct some issues with my answer. Sorry for the incovenience.

1 Like

Okay, I think I fixed that one.records[id].hasOwnProperty(“tracks”)

You have to use bracket notation to access the value. Now that I fixed it, the test checks it off. Still trying to figure the other one out.

1 Like

I thought I was creating a blank array with

const tracks = [];

and then pushing value to it with

records[id][prop].push(value);

I’m thinking maybe I’m not referencing something correct.

} else if (prop == “tracks” && !id.hasOwnProperty(“tracks”)) {

The id parameter represents a number that identifies a record in the recordCollection object, so it doesn’t have the hasOwnProperty() method. Instead, you should check if the record with the specified id has the tracks property, like this:
} else if (prop == “tracks” && !records[id].hasOwnProperty(“tracks”)) {

With this change, the updateRecords() function should work correctly.

So I tried console.log() and it’s giving me the correct value in the new array so now I’m really stumped as to why it’s not working. I also changed my new array to this

let tracks = [value];

So the console.log() is reading [‘Take a Chance on Me’] which should be correct right? Because it’s the last and only element as stated.

You have a new array. But that array has no connection to ‘records’. What part of ‘records’ should be modified when you are done with that case?

1 Like

I figured it out. I had to create a connection for the new array to the properties so it would know to update the properties section to include the new array. Thanks for the help!

records[id][prop] = (tracks);

1 Like

Your variable tracks really is being assigned a blank array, but it is never used. You’re right about the referencing situation. Keep in mind that giving a variable the name “tracks” does not make it a reference to the record collection or its properties in any way.

Are you sure you need a new variable (which you declared with the keyword const), or are you just trying to make it so the record itself has a new value? Perhaps you may consider an assignment approach. Remember you can also use the assignment operator (‘=’) with object properties.
Edit: I just realized I might be a bit late to the answer, haha. Regardless, I’m happy you have figured out a solution.

1 Like