So I’ve been stuck on record collection for at least a week now. Went back through the javascript object lessons again. Looked at answers on the FCC forum. It’s still not clicking for me.
Is anyone able to help me out here? I took a few days off to see if it would help but I think it made me more confused, lmao. This is my third iteration of code, and I’m not passing any of thetests. Here’s what I have so far:
My 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' && value !== ''){
object[id][prop] = value;
} else if (prop == 'tracks'){
if (object[id].hasOwnProperty('tracks') == false){
object[id][prop] == [value];
} else if (prop == 'tracks' && value !== ''){
tracks.push(value);
}
} else if (value == ''){
delete object[id][prop];
}
return object;
}
updateRecords(collection, 5439, 'artist', 'ABBA');
console.log(artist);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36.
Ok, update: I’m now passing every test except “After updateRecords(collection, 5439, "tracks", "Take a Chance on Me") , tracks should have Take a Chance on Me as the last element.” with the following code:
function updateRecords(object, id, prop, value) {
if (prop === 'tracks' && object[id][prop] === false){
object[id][prop] = [value];
} else if (prop != 'tracks' && value != ''){
object[id][prop] = value;
} else if (value ===''){
delete object[id][prop];
} else if(prop === 'tracks'){
object[id][prop].push(value);
}
return object;
}
I am getting “TypeError: Cannot read property ‘push’ of undefined” and I’m not sure what I did wrong. Does this tell me that object[id][prop] is not an array in this instance?
That test checks that you can un-set the tracks. But once you have un-set it, there is no array to push onto, which is why you are getting that error about undefined not having the push method.
You’ve got the right idea with the first if clause, the === false just isn’t doing what you need/expect it to.
‘A considerable amount of guessing’ until it works is a fair part of my strategy. Planning is key to coding, but fiddling around until you get something that works is super important.
Feel free to ask if there are parts we can clarify.