Dear admuh
I had added the comma and below are my retyped codes:
// 设置
const recordCollection = {
2548: {
albumTitle: 'Slippery When Wet',
artist: "",
tracks:[]
},
2468: {
albumTitle: '1999',
artist: 'Prince',
tracks: ['1999', 'Little Red Corvette']
},
1245: {
albumTitle:"Riptide",
artist: 'Robert Palmer',
tracks: ["Addited to Love"]
},
5439: {
albumTitle: 'ABBA Gold',
artist:"ABBA",
tracks:["Take a Chance on Me"]
}
};
// 只修改这一行下面的代码
function updateRecords(records, id, prop, value) {
if (prop!=='tracks'&&value!=="") {
records[id][prop]=value;
} else if (prop==="tracks"&& records[id].hasOwnProperty("tracks")) {
records[id][prop]=[value];
} else if (prop==="tracks"&&value!=="") {
records[id][prop].push(value);
} else if (value==="") {
delete records[id][prop];
} return records;
}
updateRecords(recordCollection, 5439, "artist", "ABBA");
console.log(updateRecords(recordCollection, 5439,"artist", "ABBA"))
updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me")
console.log(updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me"))
updateRecords(recordCollection, 2548, "artist", "");
console.log(updateRecords(recordCollection, 2548,"artist",""))
updateRecords(recordCollection, 1245, "tracks", "Addicted to Love")
console.log(updateRecords(recordCollection, 1245, "tracks", "Addicted to Love"))
updateRecords(recordCollection, 2468, "tracks", "Free")
console.log(updateRecords(recordCollection, 2468, "tracks", "Free"))
updateRecords(recordCollection, 2548, "tracks", "")
console.log(updateRecords(recordCollection, 2548, "tracks", ""))
updateRecords(recordCollection, 1245, "albumTitle", "Riptide")
console.log(updateRecords(recordCollection, 1245, "albumTitle", "Riptide"))
After run the test their responses are below and 2,5,6 items are failled.
-
Passed: 1.After updateRecords(recordCollection, 5439, "artist", "ABBA")
, artist
should be the string ABBA
-
Failed: 2.After updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me")
, tracks
should have the string Take a Chance on Me
as the last and only element.
-
Passed: 3.After updateRecords(recordCollection, 2548, "artist", "")
, artist
should not be set
-
Passed: 4.After updateRecords(recordCollection, 1245, "tracks", "Addicted to Love")
, tracks
should have the string Addicted to Love
as the last element.
-
Failed: 5.After updateRecords(recordCollection, 2468, "tracks", "Free")
, tracks
should have the string 1999
as the first element.
-
Failed: 6.After updateRecords(recordCollection, 2548, "tracks", "")
, tracks
should not be set
-
Passed: 7.After updateRecords(recordCollection, 1245, "albumTitle", "Riptide")
, albumTitle
should be the string Riptide
Why the 2,5,6 items are failed? Could you help me?many thanks!
hwapipi