Hi, all! Below you can find the code I wrote. The thing is I first wrote it without the first return statement and after running the tests and failing I realized that after deleting the tracks property I was creating it again further down the code. So I added this return statement to exit the code. Would any of you have an idea on how I could do it so I don’t need to have to equal lines “return collections;”? Thanks!
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"
}
};
// Only change code below this line
function updateRecords(id, prop, value) {
if (value === "") {
delete collection[id][prop]
return collection;
}
if (prop !== "tracks" && value != "") {
collection[id][prop] = value
}
if (prop === "tracks") {
if (collection[id].tracks === undefined){
collection[id][prop] = []
}
collection[id].tracks.push(value)
}
return collection;
}
updateRecords(5439, "artist", "ABBA");
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36.
Your style is making it hard to see what’s going on in your code:
function updateRecords(id, prop, value) {
// Delete property if value is empty string
if (value === "") {
delete collection[id][prop]
return collection;
}
// Set prop to value if not a track
// ***************************************************************
// Note: If you used an else-if you wouldn't need && value "== ""
// Also note: You should *always* use strict comparison (so not != or ==)
// ***************************************************************
if (prop !== "tracks" && value != "") {
collection[id][prop] = value
}
// If property is tracks, add track
// ***************************************************************
// Note: you didn't exclude value === ""
// so you need the return above so you don't execute this clause
// This is also where an else-if would be great
// ***************************************************************
if (prop === "tracks") {
// Check if no tracks yet
if (collection[id].tracks === undefined) {
collection[id][prop] = []
}
// Push value onto tracks
collection[id].tracks.push(value)
}
// Return
return collection;
}
Many thanks guys! So just to double check you would suggest me to re-write the code in the following way?:
function updateRecords(id, prop, value) {
// Delete property if value is empty string
if (value === "") {
delete collection[id][prop]
} else if (prop !== "tracks") { //Set prop to value if not a track
collection[id][prop] = value
} else if (prop === "tracks") { //If property is tracks, add track
// Check if no tracks yet
if (collection[id].tracks === undefined) {
collection[id][prop] = []
}
// Push value onto tracks
collection[id].tracks.push(value)
}
// Return
return collection;
}