I’ve been working on the Record Collection Challenge . When I run the tests, almost none of the assignments are validated although everything seems to work fine according to what I can see in my console.
I have copied my entire code below. If you have any idea why it is not accepted by the validation system, I’d be very grateful for your hints
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 prop isn't tracks and value isn't an empty string, update or set that album's prop to value.
if (prop != "tracks" && value != "") {
recordCollection[id][prop] = value;
}
//If prop is tracks but the album doesn't have a tracks property, create an empty array and add value to it.
else if (
prop == "tracks" &&
recordCollection[id].hasOwnProperty("tracks") == false
) {
recordCollection[id][prop] = [];
recordCollection[id][prop].push(value);
}
// If prop is tracks and value isn't an empty string, add value to the end of the album's existing tracks array.
else if (prop == "tracks" && value != "") {
recordCollection[id][prop].push(value);
}
//If value is an empty string, delete the given prop property from the album.
else if (value == "") {
delete recordCollection[id][prop];
}
}
//Your function must always return the entire record collection object.
return records;
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36
I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
This was the issue I was having as well, but I don’t understand how it works this way.
My understanding is that to reference a property contained within an object you use the object’s name as the first part of the path. So in this case, how does the function know that it should reference the recordCollection object when the name of the object isn’t used? How do I know if I should use something like records instead?
The first parameter in the updateRecords definition is records (function definition)
function updateRecords(records, id, prop, value) {}
So inside the function records is the same as recordCollection
Whenever you see a function accepting parameters you should always use the parameters. Even if the value exists outside the function if they are passed to the function that is the value you should use. That is the point of passing the value.
A “true” function accepts and returns values, they do not just run and cause side effects. Such a “function” (not a function) is sometimes called a procedure but you won’t hear that in JS land very often and all JS functions return a value (i.e. undefined if nothing else).