Basic JavaScript - Record Collection

Tell us what’s happening:
This sucks but I’ve already spent a few days on this step and my brain has already turned into a giant mush even though I’m not yet close to passing this step. Anyone has any spare brain cells to lend me Id very much appreciate it but any advice on how to pass this step will also be helpful!
:exploding_head: :exploding_head: :exploding_head: :exploding_head:
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(value = " "){
    delete records[id][prop];
    
}  else if (prop !== "tracks" && value !== " ") {
    records[id][prop] = value;
} else if(props === "tracks" && value !== ""){
    records[id][prop].push(value);
}

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

Challenge: Basic JavaScript - Record Collection

Link to the challenge:

What does = do vs === again?

1 Like

What the!
I can’t believe I forgot about that sneaky guy!
Thank you! Thank you! thank you!!! :smile: :smile:
Problem is when I change the ‘==’ the first condition is met but the third one(delete…) which worked before changing it reverts to incomplete… :sob:

Well, check here now. You have two issues, a typo and a logic issue (what if there is no array!).

1 Like

Thank you for your help!
I don’t think I understand it so well, my code isn’t passing here’s how I changed it based one what I thought would work:

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

Thank you very much for your time!

Can you describe what this is intended to do, line by line?

If an array called “tracks” doesn’t exist in the object then the else statement should be executed.

  • First I defined an array.

  • Then I added the value to it.

  • then I added a property called “tracks” to my object.

  • And gave it a value of the array I created.

Basically I’m trying to do this:

.
Thank you again I have no words :smile:!

Did you?

1 Like

Adding tracks to the id property. Isn’t assigning it gonna add it?
BTW am I doing it the proper way or is there a shorter way to add an array to an object. I tried googling it but didn’t found an answer.

This line literally says “set the value of records[id] equal to the string literal "tracks"”.

1 Like

Thank you for your fast reply.
I tried a few ways but I don’t get how I’m supposed to add an object to an array. All searches tell me to add a property by typing the object followed by a property name and then = the value.

Yes. So you are adding records[id] to the records object with the value "tracks".

1 Like

Forgive me for so much text.

Based on your description and corrections, your code looks like this, right?

// Only change code below this line
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 !== ""){
    if(records.hasOwnProperty(prop)){
      records[id][prop].push(value);
    }
    else{
      var array = [];
      array.push(value);
      records[id] = "tracks";
      records[id][tracks] = array;
    }
  }

return records;
}

Well, let’s focus on the second else if:

First conditional

   if(records.hasOwnProperty(prop)){
     records[id][prop].push(value); -> This will search for the existence of a property within the main part (root) of the 'recordCollection' array. (This search does not extend into nested objects, meaning it focuses only on checking if the property exists within the root of the array.)
   }

Second conditional

   else{
     var array = []; -> creates an array
     array.push(value); -> adds the value
     records[id] = "tracks"; -> replaces the key value id for 'tracks'
     records[id][tracks] = array; -> creates and sets the key 'tracks'
   }}

So, for the first conditional, you aren’t searching for a specific id, let me add an example:

Let’s say I have an array of objects called ‘musicCollection’:

const musicCollection = {
  1: {
    musicName: 'name3',
    artist: 'Artist 3',
    tracks: ['Track 5', 'Track 6']
  },
  2: {
    musicName: 'name3',
    artist: 'Artist 3',
    tracks: ['Track 5', 'Track 6']
  },
  3: {
    musicName: 'name3',
    artist: 'Artist 3',
    tracks: ['Track 5', 'Track 6'],
    limitedEdition: true
  }
}

and I want to verify if the property 'limitedEdition' exists. So we gone make use of 'hasOwnProperty('limitedEdition')'. Although we may have two scenarios:

First scenarios:

We don’t specify the [id], so 'hasOwnProperty' will only search the properties located in the root (1,2 and 3):

const musicCollection = {

  1: {
    ...properties
  },
  2: {
    ...properties
  },
  3: {
    ...properties
  }

}

Second scenarios:

We Specify the id, so 'hasOwnProperty' will iterate over the properties of the specified id:

const musicCollection = {

  1: {
    ...properties
  },
  2: {
    ...properties
  },
  3: {
    musicName: 'name3',
    albumTitle: 'Album 3',
    artist: 'Artist 3',
    tracks: ['Track 5', 'Track 6']
    limitedEdition: true -> exists
  }

}

  musicCollection[3].hasOwnProperty('limitedEdition') -> true:

Try to understand and use it

Last, your 'else' condition:

   else{
     var array = []; -> creates an array
     array.push(value); -> adds the value
     records[id] = "tracks"; -> replaces the key value id for 'tracks'
     records[id][tracks] = array; -> creates and sets the key 'tracks'
   }}

The problem here, is that records[id] = "tracks"; replaces the key [id] for 'tracks':

  1245: {
    artist: 'Robert Palmer',
    tracks: []
  },
  "tracks": { -> It may end like this, replacing the key [id]
    albumTitle: 'ABBA Gold'
  }

So just remove the line records[id] = "tracks";, since 'records[id][tracks] = array;', is already doing the same (creating the key and setting the value, a one-line)

Last, you aren’t accessing correctly the property 'records[id][tracks] = array;'. I will provide a link that explains how to access dynamic properties

Try to understand it, make the adjustments and it should work, if not ask again.

Cheers

Thank you so much!
You have no idea how much explanation helped me. That was much clearer then all explanations I researched on google. I finally really understood what was going on in my code! I have not enough words to express my appreciation for your kindness :smiling_face:!

Happy to help! :computer:

Don’t hesitate to ask again if you have more questions. The community will surely answer your questions

Cheers

1 Like

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