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:
Learn to code. Build projects. Earn certifications.Since 2015, 40,000 graduates have gotten jobs at tech companies including Google, Apple, Amazon, and Microsoft.
Erma32
August 5, 2020, 2:36pm
2
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.
1 Like
ilenia
August 5, 2020, 2:37pm
3
moshiurrahman92:
collection[id][0]
collection[id]
is an object, it doesn’t a 0
property
you are creating this array but then you do nothing with it
moshiurrahman92:
collection.id[2]
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.
ilenia:
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
yeah so what to do now?
ilenia
August 5, 2020, 2:50pm
5
you need the array in the right place inside collection
, that’s just there with a value inside and doing nothing
moshiurrahman92:
yeah so what to do now?
try reviewing how to manipolate complex objects
Learn to Code — For Free
I have read the article but can’t get anything to solve the challenge