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).
@JeremyLT I noticed your recently closed topic/update/revamp for the Record Collection question. TBH I’ve struggled with the wording and the logical flow of the requirements listed. I resisted checking the solutions for quite a while, but eventually looked. I’ve since adopted a similar single conditional term for each if (…) block, which has drastically simplified my solution to this:
function updateRecords(records, id, prop, value) {
if (value == "") {
delete records[id][prop];
}
else {
if (prop == "tracks") {
if (records[id].hasProperty("tracks")) {
//*** Do nothing, aka "no operation" aka "nop"
}
else {
records[id][prop] = [];
}
records[id][prop].push(value);
}
else {
records[id][prop] = value;
}
}
return records;
}
My main question for this is: with the introduction of arr.hasOwnProperty("abc") earlier in the lesson, why is there not a solution that actually uses this?
I also think the list of requirements could be more newbie friendly, but I also think it is a good indication that real-life requirements can often be initially presented in a way that won’t mimic the logic of the solution. Perhaps a second exercise could retain the original (or current) order of requirements, with a simpler first exercise that forces usage of recently introduced concepts.
When possible, I would highly suggest avoiding the if blocks where nothing is happening. You can simplify this even further by just checking to see if the property doesn’t exist within the object and avoid a useless if-else statement:
I see the difference now in the requirements. I’ve been on this for several weeks now (due to limited time) and somehow missed the update. Using console.log helped a lot.
Due to the scrolling in the solution, I also missed that hasOwnProperty was offered.
Thank you!
I got so stuck on this, specifically because in the output it only states what output it was expecting. So I set up a thousand console log lines to make sure that these outputs matched mine, and it did for every prompt!
I could not figure out what it was because I got so used to that Collection name… But it makes sense! Moving on, thanks again!