Tell us what’s happening:
I’m having good amount of difficulty understanding the instructions. I read several topics created and their solution.
I can’t say I have perfect understanding of the problem yet.
now after I wrote and corrected almost the whole thing based on what I found in other ppl solutions, I still get error #error
After updateRecords(5439, “tracks”, “Take a Chance on Me”), tracks should have “Take a Chance on Me” as the last element.
…
I’m not sure if I’m on the right track. I don’t have any coding background and I wonder if just working with freecodecamp is good enough??
plz advise me .
tnx.
Your code so far
// 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!==""){
collection[id][prop]=value;
}
else if (prop==="tracks"&&collection[id].hasOwnProperty["tracks"]){
collection[id][prop]=[];
collection[id][prop].push(value);
}
else if(prop==="tracks"&&value!=="")
{collection[id][prop].push(value);}
else if (value===""){
delete collection[id][prop];
}
return collection;
}
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.2 Safari/605.1.15.
else if (prop==="tracks"&&!collection[id].hasOwnProperty["tracks"]){
collection[id][prop]=[];
collection[id][prop].push(value);
}
I fix this and 2 new errors show up
//
After updateRecords(2468, “tracks”, “Free”), tracks should have “1999” as the first element.
After updateRecords(2548, “tracks”, “”), tracks should not be set
for the 1st, it should solve this
//If prop is “tracks” but the album doesn’t have a “tracks” property, create an empty array before adding the new value to the album’s corresponding property.
for the 2nd, I think it goes with this
//If prop is “tracks” and value isn’t empty (""), push the value onto the end of the album’s existing tracks array.
You’re doing the opposite. collection[id].hasOwnProperty["tracks"] is true if the album DOES have the tracks property. Then you replace it with an empty array.
That means that your next else if only runs if the album DOES NOT have the tracks property. You then attempt to push to an undefined value, which causes an error.
I just wanted to make sure, because you said you were getting a different error in the previous post.
The instructions state “If value is empty (”"), delete the given prop property from the album.". That means since the value argument is a blank string, your code should be deleting the tracks property from record “2468” after the above test runs. Unfortunately, it does not do that, hence the failing test.
It would if the else if (seen below) before the else did not evaluate to true. In the test, your code adds a blank string element to the end of the tracks array.
else if (prop === "tracks" && collection[id].hasOwnProperty("tracks")) {
collection[id][prop].push(value);
}
Instead of deleting/modifying/adding new code, I suggest you write out your algorithm for this challenge first in plain language. Write down the exact steps you would take if you were the computer. That means you need to make sure your logic will work on paper with each test case before writing ANY code. At this point you are just guessing at a solution by changing bits of code here and there, instead of understanding what the algorithm should be for you to be able to write the correct code.
An algorithm is just the logical step by step process you need to perform to get to the correct response.