Hey guys,
I’ve been so confused the whole day by the Record Collection challenge in the JavaScript section. I ended up using the hints, trying some more, searching on here etc but ultimately had to check the solution before trying again, and still I had to use the solution again to fix it!
Here’s what I have. Could somebody explain why, within the two lines of “collection[id][prop] = [value];”, one must have square brackets and the other not? I was trying for so long before discovering that having square brackets on both or neither would fail the test and I can’t see why.
function updateRecords(id, prop, value) {
if (prop == “tracks” && value !== “”) {
if (collection[id][prop]) {
collection[id][prop].push(value);
} else {
// v This line v
collection[id][prop] = [value];
}
} else if (prop !== “tracks” && value !== “”) {
// v And this line v
collection[id][prop] = value;
} else {
delete collection[id][prop];
}
return collection;
}
Thanks to anyone who can clear this up for me
// 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"
}
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));
// Only change code below this line
function updateRecords(id, prop, value) {
if (prop == "tracks" && value !== "") {
if (collection[id][prop]) {
collection[id][prop].push(value);
} else {
// v This line v
collection[id][prop] = [value];
}
} else if (prop !== "tracks" && value !== "") {
// v And this line v
collection[id][prop] = value;
} else {
delete collection[id][prop];
}
return collection;
}
// Alter values below to test your code
console.log(updateRecords(5439, "artist", "ABBA"));
Your browser information:
User Agent is: Mozilla/5.0 (X11; CrOS x86_64 10718.88.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.118 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection