// 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
}
if(prop === "tracks"){
if(collection[id].hasOwnProperty("tracks") === false){
collection[id][prop] = []
collection[id][prop].push(value)
}
}
if(value === ""){
let power = delete collection[id]
return power
}
return collection
}
// Alter values below to test your code
console.log(updateRecords(5439, "artist", "ABBA"));
These three are not passing.
What if it does have a “tracks” property? Right now you do nothing.
You did many mistakes in your code .
- In your first if statement =>
if(prop !== "tracks" && value){
you have to check that value is not empty i.e.
if(prop !== "tracks" && value!=""){
.
- In your second if statement you did two mistakes.
- In line
if(prop === "tracks"){
you have to check that prop is equal to tracks and value not equal to empty i.e.
if(prop === "tracks" && value!==""){
.
- here you need to check for condition as following :
if(collection[id].hasOwnProperty(prop)){
collection[id].tracks.push(value)
}else{
collection[id].tracks=[]
collection[id].tracks.push(value)
}
- In your third if statement you have to delete
collection[id][prop]
and no need to store it in a variable or returning it.
- In the place of many if statements you have to write
else if
statement for checking conditions because multiple if
statement can cause of error.
1 Like
I’ve updated the 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(prop !== "tracks" && value !== ""){
collection[id][prop] = value
}
if(prop === "tracks"){
if(collection[id].hasOwnProperty("tracks")){
return collection[id][prop]
}
else{
collection[id][prop] = []
collection[id][prop].push(value)
}
}
if(value === ""){
delete collection[id][prop]
}
return collection;
}
// Alter values below to test your code
console.log(updateRecords(1245, "album", "Riptide"));
But there are problems
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.
After updateRecords(2548, "tracks", ""), tracks should not be set
Yeah I got it delete removes the property in an object and when assigned to a variable it returns “true”
That was the mistake i’ve been doing