How does this code work, can anyone explain to me in detail?

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(prop === "tracks" && value !== ""){
    
  }
  return records;
}

updateRecords(recordCollection, 5439, 'artist', 'ABBA');
updateRecords(1, 2, 3, 4);
  • Your function must always return the entire records object.
  • If value is an empty string, delete the given prop property from the album.
  • 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.

Of course this can’t be done from a task, so my question is: why can’t I add my own values?
How to understand and complete this task on your own, is it possible to understand this?

Can you elaborate on what you mean?

While you have translated the instruction correctly, the && part is unnecessary. (Bonus: Do you know why?)

likewise, this can be (almost) entirely skipped. (Again, why?)

“How does this code work”… well at the moment, it doesnt (because it doesnt fulfil the last bullet point.

“Как работает этот код”… ну, на данный момент это не так (потому что он не соответствует последнему пункту.
I can’t figure out how to do this last point

Okay. Let’s break it down a little bit:

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.

Just for my eyes sake…

  • 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.

(My brain so wants to do this all in one line, but… the course you’re taking wants them as separate instructions; so be it. Time for colors and questions!)

Which part(s) are you having trouble with?

  • First, if the album does not have a tracks property, assign it an empty array.

You know how to construct an if. You’ve shown that to us already.
Do you know how to check if an object has a property?
Do you know how to create an empty array and assign it to something?

  • Then add the value as the last item in the album’s tracks array.

This means to do it after the if above is finished.
Do you know how to add an item to an array?

yes records.id[tracks] = “one”; i think tat

  • Во-первых, если у album нет tracks свойства, присвойте ему пустой массив.
    one by one if you look at it, I don’t understand it

I assume you’re responding to Purple there. In which case, have a look at https://www.w3schools.com/jsref/jsref_push.asp

For blue, there’s a few ways to go about it

For green, an empty array can be created by just setting the value equal to [].

I understood this, but I just don’t understand how to write it in code

I think I solved it with this code:

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"){  // no need for the && due to the previous condition
        records[id][prop] = value;
    }
    else {  // no need for the else if since there is no other option
      if(records[id][prop]){  // Check to see if there's a 'track' prop
        records[id][prop].push(value); // Push the value to the 'track' array
      }
      else{
        records[id] = {...records[id], tracks:[],}; // Create an empty 'track' array inside the object
        records[id][prop].push(value); // Push the value to the 'track' array
      }
    }
    return records;
  }
  //testing all 3 bulletpoints 
updateRecords(recordCollection, 5439, 'artist', 'ABBA');
updateRecords(recordCollection, 2468, "tracks", "");
updateRecords(recordCollection, 5439, 'tracks', 'Dancing Queen');
updateRecords(recordCollection, 2548, 'tracks', 'Bed Of Roses');

I tested it with console.log, seems to print the correct results.
As for :

updateRecords(1, 2, 3, 4);

This returns 1 when I printed it, don’t know if that is the desired outcome, since there is no data for the argument 1.

I hope I’m correct with this code and explanation.

You can simplify that a little bit, if you follow specifically what they say, but that code will work.

(Hint: If there was no tracks property; create it. (end of sentence) then, regardless of what happened before, add the value to the track array.)

Thank you!

You said before that you are tempted to do it all in one line of code. If you can, I would love to see how it’s possible :slight_smile:

else { records[id]['tracks'] = [...(records[id]['tracks'] ?? []),value]; }

(You can spread an empty array. You just get nothing out.)

This is very elegant!
This nullish coalescing operator “??” is new to me, that’s cool!
Now I’m even happier, got to play with some code and on top of it got to learn something new :star_struck:

2 Likes