A little Help please

Tell us what’s happening:
My code is failing this test
After updateRecords(2468, "tracks", "Free") , tracks should have "1999" as the first element.

Your code so far


// Setup
var collection = {
2548: {
  album: "Slippery When Wet",
  artist: "Bon Jovi",
  tracks: [
    "Let It Rock",
    "You Give Love a Bad Name"
  ]
},
2468: {
  album: "1999",
  artist: "Prince",
  tracks: [
    "1999",
    "Little Red Corvette"
  ]
},
1245: {
  artist: "Robert Palmer",
  tracks: [ ]
},
5439: {
  album: "ABBA Gold"
}
};

// Only change code below this line
function updateRecords(id, prop, value) {
if(value === ""){
  delete collection[id][prop];
}else if(prop === "tracks"){
  if(collection[id][prop] === []){
    collection[id][prop].push(value)
  }else{
    collection[id][prop] = [];
    collection[id][prop].push(value);
  }
}else{
  collection[id][prop] = value;
}

return collection;
}

updateRecords(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/80.0.3987.149 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

I think the problem you are facing may be related to your initializing a tracks property that already exists, in that first else statement.

There is a logic error in your else if statement.

In this statement:

  if(collection[id][prop] === []){  
    collection[id][prop].push(value)
  }

you are looking for an array that is empty. But this doesn’t exist - the collection either has a tracks property with an array of track names, or the track property doesn’t exist. This means the code in these curly brackets never executes.

This means your default always executes:

else{
    collection[id][prop] = [];
    collection[id][prop].push(value);
  }

and this code resets the tracks property to empty, which erases all track names already stored.

The instructions say: If the prop === “tracks” the instructions say you need to either create an empty array if the album doesn’t have a tracks property before adding the new value OR push the value onto the existing tracks arrays. Your code is currently always creating an empty array and then adding the new value, so check the if statement I quoted above and see if you can fix it to push the value onto the existing tracks array.

Hope that helps!

1 Like