Little help needed with Record Collection

Tell us what’s happening:
I have run my code . There is one test which is run twice to push a value to the end of the array , fails once. it is the one I have labelled FAILED in the comments.

Why does it fail just once? Why doesn’t it fail twice? If I comment out that section the code fails twice on the push task.

I have commented each section of code and which task it is supposed to perform.

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 (prop === "tracks" && object[id].hasOwnProperty(prop) === false) {
    /** If prop is tracks but the album doesn't have a tracks property, create an empty array and add value to it */
    object[id][prop] = value;  
  } else if (prop !== "tracks" && value !== '') {
    /** If prop isn't tracks and value isn't an empty string, update or set that album's prop to value */
    object[id][prop] = value;   
  } else if (prop === "tracks" && value !== '') {
    /** FAILED If prop is tracks and value isn't an empty string, add value to the end of the album's existing tracks array */
    object[id][prop].push(value);
  } else if (value === '') {
    /** If value is an empty string, delete the given prop property from the album */
   delete object[id][prop]; 
}

   return object;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

Actually, I think its this line that is your problem. You are not creating an array here, but the tracks property must be an array.

As a side note, your formatting is nonstandard and makes it a little bit harder for others to read your code. This is the usual way to format if-else statements:

// Only change code below this line
function updateRecords(object, id, prop, value) {
  if (prop === "tracks" && object[id].hasOwnProperty(prop) === false) {
    /** If prop is tracks but the album doesn't have a tracks property, create an empty array and add value to it */
    object[id][prop] = value;  
  } else if (prop !== "tracks" && value !== '') {
    /** If prop isn't tracks and value isn't an empty string, update or set that album's prop to value */
    object[id][prop] = value;   
  } else if (prop === "tracks" && value !== '') {
    /** If prop is tracks and value isn't an empty string, add value to the end of the album's existing tracks array */
    object[id][prop].push(value);
  } else if (value === '') {
    /** If value is an empty string, delete the given prop property from the album */
   delete object[id][prop]; 
  }
  return object;
}

Thanks for the quick reply. I will change the formatting.

Could you please expand on your answer about the error you found.

I thought that the issue was with the next instruction the one marked “FAILED”!

I have a bit of an advantage because I’ve done this problem before, so explaining how I knew where to look isn’t super helpful.

If the problem was new to me, I’d start with the error message

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

This would make me want to see what happens on that function call, so then I’d add

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

at the bottom of the code.

This yields

{ '1245':
   { artist: 'Robert Palmer',
     tracks: [] },
  '2468': 
   { albumTitle: '1999',
     artist: 'Prince',
     tracks: [ '1999', 'Little Red Corvette' ] },
  '2548': 
   { albumTitle: 'Slippery When Wet',
     artist: 'Bon Jovi',
     tracks: [ 'Let It Rock', 'You Give Love a Bad Name' ] },
  '5439':
   { albumTitle: 'ABBA Gold',
     tracks: 'Take a Chance on Me' } }

The tracks for 5439 is not an array, but all of the other tracks are.

There is only one entry in the tracks for 5439 and the original collection does not have any tracks for 5439, so there must have been something fishy when the property was created.

That would lead me to the case covered by

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

And then I’d see that the property is created with a value instead of a value in an array.

1 Like