I get the right output but couldn’t clear this test. However I change my code fourth and fifth conditions fail. Is there any mistake in my code?
Failed conditions:
- After updateRecords(1245, “tracks”, “Addicted to Love”), tracks should have “Addicted to Love” as the last element.
- After updateRecords(2468, “tracks”, “Free”), tracks should have “1999” as the first element
// 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"
},
add:function add(id,prop,value){
[].push.call(this[id][prop],value);
}
};
// 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(collection[id].hasOwnProperty(prop)){
if(value==""){
delete collection[id][prop];
//alert("good day sir");
}else{
if(prop=="tracks"){
collection.add(id,prop,value);
}else{
collection[id][prop]=value;
}
}
}else{
if(prop=="tracks"){
collection[id][prop]=[];
collection[id][prop].push(value);
}else{
collection[id][prop]=value;
}
}
return collection;
}
// Alter values below to test your code
updateRecords(1245, "tracks", "Addicted to Love");
// 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"
},
add:function add(id,prop,value){
[].push.call(this[id][prop],value);
}
};
// 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(collection[id].hasOwnProperty(prop)){
if(value==""){
delete collection[id][prop];
//alert("good day sir");
}else{
if(prop=="tracks"){
collection.add(id,prop,value);
}else{
collection[id][prop]=value;
}
}
}else{
if(prop=="tracks"){
collection[id][prop]=[];
collection[id][prop].push(value);
}else{
collection[id][prop]=value;
}
}
return collection;
}
// Alter values below to test your code
updateRecords(1245, "tracks", "Addicted to Love");