Challenge Record Collection has an issue.
User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
.
Please describe how to reproduce this issue, and include links to screenshots if possible.
My code:
// 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( value !== ""){
if(prop!=="tracks"){
collection[id][prop]=value;
}
else if( prop ==="tracks"){
collection[id][prop].push(value);
}
}
else
delete collection[id][prop];
return collection;
}
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
@pragya68 It works perfectly for me !!!. You can clear your browser caches and cookies and resubmit .
It's okay, sometimes we do get stuck and console.log
helps a lot to determine what the problem could be. The assignment looks okay, I'll talk about what is going wrong with your code. It is possible that the underlying concept is not explained to you already but with this conversation, you would be able to proceed.
updateRecords(5439, "tracks", "Take a Chance on Me");
TypeError: Cannot read property 'push' of undefined
collection[id][prop].push(value);
I can add more hints if you need more help on this.
p.s. I am not fixing your code by intention, as learning is what is needed here.
@pragya68 what @crashuniverse suggests is a good way to debug your issues in the future. The issue is because you're pushing to a non-existent tracks
property for record 5439. It was intended for the user to check for this, but this requirement was a bit implied and should probably be more explicitly stated.
In between these two lines, this line of code needs to be put:
"If <code>\"tracks\"</code> is empty before you update it, create an empty array before pushing to it.",
This change should look like this:
Please first read the contributing guidelines before taking care of this issue. And feel free to visit the Contributors Help chat room if you have any questions about helping. We're there to help.
@erictleung Finally you have acknowledged this issue maybe now you can understand my fix? fking GG XD
still can't resolve the issue, i tried creating an empty array before updating, same error. even tried clearing my cach on chrome and firefox, still the same error:
"updateRecords(5439, "tracks", "Take a Chance on Me"), tracks should have "Take a Chance on Me" as the last element."
I noticed that a lot of people were having problems with this challenge, which is why I wrote a short blog post on CodePen about possible solutions, I hope you and others would find it helpful. Here's the URL: https://codepen.io/profoundcoder/post/record-collection
Yes i tried solutions 1 2 and 3 but i keep getting this error: updateRecords(5439, "tracks", "Take a Chance on Me"), tracks should have "Take a Chance on Me" as the last element.
seems like a cache thing, but i cleared all my browser's cache. still the same error.
please check the attached screenshot and look on the left side error number 2
It worked now, someone from gitter suggest this: collection[id][[prop] = [value];
What @erictleung suggests is a good indicator for user to know what needs to be done. If we all agree, I can pick it up.
@mav1283 What you say will work only if collection[id]
is an object and exists already so that you can access its propery prop
.
I am taking this up. I'll enhance something on lines of what @erictleung suggested.
@crashuniverse Your comment about collection[id] being an object and already existing was a crucial hint for me to unlock this problem.
I really love how you wrote this code out.
I have updated my post on CodePen: https://codepen.io/profoundcoder/post/record-collection
@mav1283 , could you recheck your solution, I tried it and it did not work for me, here is a screenshot:
Sorry, I mixed up the Solution Code, here is @mav1283 's
hey i must have forgotten this already, try this one
function updateRecords(id, prop, value) {
if(value !== ""){
if(prop !== "tracks"){
collection[id][prop] = value;
} else if(prop === "tracks"){
if(collection[id].tracks){
collection[id].tracks.push(value);
} else {
collection[id].tracks = [];
collection[id].tracks.push(value);
}
}
} else {
delete collection[id][prop];
}
return collection;
}
Most helpful comment
It worked now, someone from gitter suggest this: collection[id][[prop] = [value];