Record Collection Walkthrough

I am currently working on this Record Collection challenge and I am meeting all of the requirements aside from one. I know this is a big ask but can someone hop on Zoom with me and walkthrough the solution. I’m not looking for the answer just a little help and understanding. It shouldn’t take more than 10 - 15 minutes. I appreciate any help in advance.

why don’t you post your code here? you will get some hints

@ieahleen

Here is what I have so far.

// Setup
var collection = {
  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(object, id, prop, value) {
  
/* If prop isn't tracks and value isn't an empty string, 
update or set that album's prop to value. */
  if (prop != "tracks" && value != "") {
    object[id][prop] = value; }

/*If prop is tracks but the album doesn't have a tracks 
property, create an empty array and add value to it. */
  else if (prop == "tracks" && "tracks" == undefined) {
    object[id][prop] = object[id][prop][value]; }

/*If prop is tracks and value isn't an empty string, 
add value to the end of the album's existing tracks array. */
  else if (prop == "tracks" && value != "") {
    object[id][prop].push(value); }

/*If value is an empty string, delete the given prop
 property from the album.*/
  else if (value == "") {
    delete object [id][prop]; }

  
  return object;
}

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

how could this ever be true?

also this line, look at it carefully

1 Like

That’s where I am stuck.

/*If prop is tracks but the album doesn't have a tracks 
property, create an empty array and add value to it. */

From what I understood, the data that is being passed through has a property of “tracks” but the object that we are updating does not have the property of “tracks”.

"tracks" == undefined

I tried to use this as a way to determine if the object that we are passing data to already has this property.

object[id][prop] = object[id][prop][value];

I’m not sure how to create an empty array, once the array is created I can push the data to it but I’m lost.

/*If prop is tracks but the album doesn't have a tracks 
property, create an empty array and add value to it. */

I found this line really confusing, but it’s asking you to check two things: Firstly, whether prop === “tracks” which you’re doing, and secondly, whether the object that we’re updating already has a tracks property. You could check this with:

if(object[id].hasOwnProperty("tracks") === false)

As for creating a new array, this would do it:

object[id][prop] = [value];

I can show you how to put it all together if you like, but I don’t want to jump the gun if you want to figure it out for yourself :slight_smile:

1 Like

No, I’m sure that I can figure it out from there. I forgot about .hasOwnProperty and that will do exactly what I need.

object[id][prop] = [value];

If we are passing the parameters of object, id, prop, & value, wouldn’t the value already be assigned to the property?

If I’m understanding correctly, if we assign a value to a property on an object, and that property doesn’t currently exist, it will be created.

Like…

let user = {
  name: "John"
}
user.age = 26;
console.log(user);

{
name:"John",
age:26
}

Yeah?

1 Like

That makes sense and I appreciate the code to show it in action.

object[id][prop] = [value];

So if this creates the property assigned to the value that we are passing if it doesn’t already exist, checking to see if it exists before we pass this would be redundant. Correct?

Not quite, because if it already exists, we don’t want to overwrite it. Basically, if it’s already there, we’ll append to it with .push() and if it’s not there, we’ll create it.

Ok that makes sense, I’ll try to work it out.

@gordon_woodbine @ilenia

I was able to successfully complete the challenge and have a better understanding now, thank you.

// Setup

var collection = {

 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(object, id, prop, value) {

 
/*If prop isn't tracks and value isn't an empty string, 
update or set that album's prop to value.*/

 if (prop != "tracks" && value != "") {
 object[id][prop] = value; }


/*If prop is tracks but the album doesn't have a tracks property, 
create an empty array and add value to it. */

 else if (prop == "tracks" && object[id].hasOwnProperty("tracks") == false) {
 object[id][prop] = [value]; }


/*If prop is tracks and value isn't an empty string, 
add value to the end of the album's existing tracks array.*/

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


/*If value is an empty string, delete the given prop 
property from the album.*/

 else if (value == "") {
 delete object [id][prop]; }


 return object;

}

console.log(updateRecords(collection, 5439, "tracks", "Take a Chance on Me"));
1 Like