https://www.freecodecamp.com/challenges/record-collection#
The solution as posted on the FCC wiki does not work. After trying a number of solutions, I looked at the wiki for help on a particular aspect of the answer which ended up being the same as my own answer. So, I copied both my code and another non-functional answer, reset the code to the problem, pasted and commented out the failed attempts, copied the solution from the wiki, and pasted in the solution on the wiki. None of the aspects of the challenge are credited
if (value !=="") {
if ([id][prop] !=="tracks") {
collection[id][prop] = value; // this part works
} else {
collection[id][prop].push(value); // no love for this part
}
}
else {
delete collection[id][prop]; // this part works
}
Another answer that didn't work:
function updateRecords(id, prop, value) {
if (prop === "tracks" && value !== "") {
collection[id][prop].push(value);
} else if (value !== ""){
collection[id][prop] = value;
} else {
delete collection[id][prop];
}
The code I see in your screenshot is not that same code I see when I go to the wiki article using the link you provided above. The code from the wiki worked fine for me.
You could try this solution here #8962
You cannot reference [id][prop] without idicating the array they are apart of. So you code will not work becausd of syntax errors
@DealPete @TopOneOfTopOne @stricknein - Sorry for posting the wrong image and yes, you're right in that it "passes", however, that still isn't right and again, it's using the (now correctly copy/pasted) code provided as the answer.
I have had this happen as well, but it was when I had tried many different things and after many different changes to the record it would not output the legit results. Try reseting the challenge then putting in the correct code and it should output what is intended.
@stricknein -- That's my standard way of doing things after I had noticed a while back that was an issue. Anyway, the above is _after_ having reset the code. The commented code below it had been added as a block.
I will look into this more to see if I can find the source of the problem. After you run this and it shows up that it was complete, when you comeback is the same output shown ? I noticed sometimes the output wouldn't change right away on some challenges.
@DealPete @TopOneOfTopOne @stricknein -- I think this happens for just about all of them and I made a new issue for it. The reason why I'm frustrated with this problem though is the code I wrote is tested as non-functional but the "correct" answer has the exact same result as mine.
And if you can't diagnose a problem, you really can't fix it.
@hrokr The real issue is your code, it's trying to push onto a non existent array at a non existent key.
If you really want to keep your code like this, you would need something like this:
function updateRecords(id, prop, value) {
if (value !== ""){
if(prop !== "tracks"){
collection[id][prop] = value;
}else{
if(!collection[id][prop]){
collection[id][prop] = [];
}
collection[id][prop].push(value);
}
}else{
delete collection[id][prop];
}
return collection;
}
Perhaps there is something wrong with the 'output' window. The code I have provided passes but the 'results' are the same as your screenshot. But verified locally the code returns the expected result.
Found a little bug: The code below is wrong but is accepted as a right answer.
function updateRecords(id, prop, value) {
if(value === ""){
delete collection[id][prop];
}else if(prop === "tracks"){
collection[id][prop] = [];
collection[id][prop].push(value);
}else {
collection[id][prop] = value;}
return collection;}
The correct answer should be the following - which works too. It checks first if the property "tracks" exists (if collection[id][prop] is true) before pushing. If it doesnt exist it creates a new array.
function updateRecords(id, prop, value) {
if(value === ""){
delete collection[id][prop]; }
else if (prop === "tracks" && collection[id][prop]){ // check if property exists
collection[id][prop].push(value);
}else if(prop === "tracks"){
collection[id][prop] = []; // initialize a new array before pushing
collection[id][prop].push(value);
} else {
collection[id][prop] = value;
} return collection;}
Here, I just updated what I wrote about it: https://codepen.io/profoundcoder/post/record-collection
No longer relevant. Closing. Please continue on Forum
Most helpful comment
Found a little bug: The code below is wrong but is accepted as a right answer.
function updateRecords(id, prop, value) {
if(value === ""){
delete collection[id][prop];
}else if(prop === "tracks"){
collection[id][prop] = [];
collection[id][prop].push(value);
}else {
collection[id][prop] = value;}
return collection;}
The correct answer should be the following - which works too. It checks first if the property "tracks" exists (if collection[id][prop] is true) before pushing. If it doesnt exist it creates a new array.
function updateRecords(id, prop, value) {
if(value === ""){
delete collection[id][prop]; }
else if (prop === "tracks" && collection[id][prop]){ // check if property exists
collection[id][prop].push(value);
}else if(prop === "tracks"){
collection[id][prop] = []; // initialize a new array before pushing
collection[id][prop].push(value);
} else {
collection[id][prop] = value;
} return collection;}