JS Code challenge not working and I don't know why

Tell us what’s happening:
I can’t find out what’s wrong in my code
for some reason

(recordCollection, 2548, "tracks", "")

does not delete the tracks property despite having the right if statement

  if(value === ""){
    delete records[id][prop];
  }

And there’s another error as well, what is wrong with this code? I really don’t get it

  **Your code so far**

// Setup
var 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(prop !== "tracks" && value !== ""){
  records[id][prop] = value;
}else if(prop === "tracks" && !records.hasOwnProperty("tracks")){
  records[id]["tracks"] = [value];
}else if(prop === "tracks" && value !== ""){
  records[id]["tracks"].push(value);
}else if(value === ""){
  delete records[id][prop];
}

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/90.0.4430.212 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

This will assign the value [""] to records[id].tracks and your final else if will not be evaluated.

But

(recordCollection, 2548, "tracks", "")

does have a “tracks” property, so the records.hasOwnProperty(“tracks”) condition will not be met hence it’ll skip to the next if, unless i’m missing something?

This isn’t doing what you think it’s doing. It’s checking whether records has a property named “tracks”, which it doesn’t.

I have the same problem but don’t get anything

Ohhh!! I meant to write !records[id].hasOwnProperty(“tracks”), everything works fine now, thanks!

2 Likes

Can I get some help too please

Create a topic with your code and people will help you

If you have a question about a specific challenge as it relates to your written code for that challenge, just click the Ask for Help button located on the challenge. It will create a new topic with all code you have written and include a link to the challenge also.

You’ll be able to edit the post so you can describe what’s going on and tell us how we can help you.

Thank you.

1 Like

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