Have I done anything wrong?

Tell us what’s happening:
Guys, I can’t get it. What’s wrong with my code. Help me to solve this challenge.

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(prop!=='tracks'&&value!==''){
delete collection[id][0];
collection[id][0]=value;
}  else if(prop==='tracks'&&collection[id][1]===''){
let arr = [];
arr.push(value);
} else if(prop==='tracks'&&value!==''){
collection.id[2].push(value);
} else if(value===''){
delete collection.id[0];
}
console.log(collection);
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/84.0.4147.105 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

Take a look at your output in the console.

'5439': { '0': 'ABBA', album: 'ABBA Gold' }

You’ve overwritten the artist property to instead be 0 and then assigned it ‘ABBA’.
Try to find where in your code this is done and refactor it to only update the value instead of the property name.

If I try to add a track to the 5439 object I get

TypeError: Cannot read property '2' of undefined

I think your code is trying to read the property 2 in your JSON but theis does not exist and as such it is undefined.

I think you’ve confused arrays and JSON objects and how to work with these. I’d suggest looking up how to call and assign JSON objects. :slight_smile:

1 Like

collection[id] is an object, it doesn’t a 0 property

you are creating this array but then you do nothing with it

collection doesn’t have a property called id, id is a variable so you can’t use dot notation

collection[id] doesn’t a 2 property

No I have created it. and look at the second where I’m adding value to the array.

yeah so what to do now?

you need the array in the right place inside collection, that’s just there with a value inside and doing nothing

try reviewing how to manipolate complex objects

I have read the article but can’t get anything to solve the challenge