Tell us what’s happening:
I am trying to complete this coding challenge and I am confused on how to add a property to an object and assign it a value using variable inputs.
If I am given a record id of 5439, I would like to be able to add the property “artist” to the object of recordCollection.id and create the artist property. I would then like to add “ABBA” to it.
If the property doesn’t already exist in the object, what is a way to create the property using a variable from input and assigning the value of that property to be another variable from the input? Please let me know if this questions needs refining.
**Your code so far**
// Setup
const 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 (value == ""){
delete(recordCollection[id][prop])
}
else if (prop != 'tracks'&& recordCollection[id][prop]) {
recordCollection[id][prop] = value;
} else if (recordCollection[id][prop] == false) {
recordCollection[id][prop] = value;
}
}
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/99.0.4844.51 Safari/537.36
I understand what you are saying here but you aren’t using the correct syntax to say it. Using recordCollection.id literally says that you want to find the property called “id” in the recordCollection object:
const recordCollection = {
id: { ... } // this isn't what you want
}
But this isn’t what you want. You want to find the property contained in the variable id. For example, if id holds the value 5439 then you want to find the property 5439 in recordCollection:
const recordCollection = {
5439: { ... } // this is what you want
}
So what other method should you use to access the property contained in a variable? Hint: There are two methods for accessing a property in an object, dot notation and bracket notation.
And you can create a new property on an object at any time by just assigning it to the object. Just like you can add an array item to an array at any time:
bbsmooth, is my use of bracket notation in my code the correct way to call for the value contained in a variable that is entered? I would need the function to update values in the objects with multiple different “id” inputs. I hope this problem statement is making sense.
Okay, so i was being jv and not using the correct object name in the function code build. In the function updateRecords, the code needs to call values from the object “records” not recordCollection. This got me a little closer to where I need to be.
Okay, so I think I have the code figured out. When I console.log all the test cases, I get the right answers, however the test algo still says I have things incorrect. Posting the updated code below:
// Setup
const 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 (value == ""){
delete(records[id][prop])
} else if (prop == 'tracks' && records[id].hasOwnProperty(prop)){
records[id][prop].push(value);
} else if (prop == 'tracks' && !records[id].hasOwnProperty(prop)) {
records[id]['tracks'] = [value];
} else if (records[id].hasOwnProperty(prop)) {
records[id][prop] = value;
} else if (records[id].hasOwnProperty(prop) == false) {
records[id][prop] = value;
console.log(records[id][prop])
}
}
updateRecords(recordCollection, 1245, 'albumTitle', 'Riptide');
console.log(recordCollection[1245])
The console.log for the Object ID called gives me the right answers, I just can’t seem to get the freecodecamp tester software to accept it. Does anyone have ideas?