updateRecords: One condition not met

Tell us what’s happening:
This is my code.
Strangely, the system tells me that the following conditions are not met , even if it does.

After updateRecords(collection, 5439, "tracks", "Take a Chance on Me") , tracks should have Take a Chance on Me as the last element.

Any advice?
Thanks

Your code 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(value==="")
  {delete object[id][prop];}


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

   else{ 
    object[id][prop]=value   
  }
 

return object;
}

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


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

Hey there!

No, it doesn’t.

The task is tracks should have X as the last element.

The line object[id]["tracks"]=value; doesn’t add as the last element (of an array).

Thanks for your reply, I still don’t get it though.

For objects without any “tracks” property, wouldn’t adding "tracks"value qualifies as “Adding as the last element of the array?”

For example,
In the record collection, object with id: 5439, doesn’t have any “tracks” property,

5439: {albumTitle: ‘ABBA Gold’ }

So I used the following line to add in the track.:

object[id][“tracks”]=value

As a result, it console log shows the following result.

‘5439’: { albumTitle: ‘ABBA Gold’, tracks: ‘Take a Chance on Me’ } }

For properties other than “tracks” properties, i used the below lines to “Add as the last element of the array”:

else if(prop===“tracks”)
{
if( object[id][“tracks”])
{
object[id].tracks.push(value);
}

Again thanks a ton.

you are creating a tracks property. But it’s something like
tracks: "Take a Chance on Me "

where is the array?

my mistake. What I meant was " as the last element" ( without the array.)

so why would my code not satisfy the condition below?

I mean After updateRecords(collection, 5439, "tracks", "Take a Chance on Me") , tracks should have Take a Chance on Me as the last element.

Because I believe it does create "Tracks " property for object with ID property 5439.

If it is wrong how should I improve it?
Thanks

the tracks property should have value of an array, where is the array in that failed case? it’s not there

So if you want to store an additional track, currently the new track would overwrite the current track, e.g.

‘5439’: { albumTitle: ‘ABBA Gold’, tracks: ‘Name of new track’ }

This wouldn’t make that much sense, because an artist/band can have multiple tracks, this is why the property is named tracks (not track).

Your current code overwrites the old track.