Tell us what’s happening:
Maybe it’s a stupid question, but how this collection object knows that we are reffering to it when we call this updateRecords function.
I needed to see a help video in this lesson. I saw for example this:
if (value === "") {
delete collection[id][prop];
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) {
return collection;
}
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36.
collection is the name of the object, id is one of the property names of the object, so for example collection[2548] will be the object held in the property names 2548, and then with collection[2548]["artist"] you are accessing the artist property on that object
the collection object doesn’t know anything, but you reference to it with its name, so it is affected
function updateRecords(id, prop, value) {
if (collection[id].hasOwnProperty("tracks")) {
/* what should happen here? */
} else {
collection[id]["tracks"]; // this does nothing
// you can't have an else if after an else
} else if (collection[id]["tracks"] !== = "")) { // extra closing ), remove it
collection[id]["tracks"].push("value")
} else if (collection[id]["prop"]["value"] === "")) { // extra closing ), remove t
delete collection[id]["prop"]
}
return collection;
}
I’ve made this in a new way but it still not working:
function updateRecords(id, prop, value) {
if (prop === "tracks"){
if (collection[id].hasOwnProperty("tracks") === false){
collection[id]["tracks"]
}
return collection;
}else if (prop === "tracks"){
if (collection[id]["tracks"] !== ""){
collection[id]["tracks"].push("value")
}
return collection;
}else if (collection[id]["prop"]["value"] === ""){
delete collection[id]["prop"]
}
return collection
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
// it shows in console that here is unexpected token
function updateRecords(id, prop, value) {
if (prop === "tracks") {
if (collection[id].hasOwnProperty("tracks") === false) {
collection[id]["tracks"] // this is still doing nothing, what do you want to do?
}
return collection; // you can remove this line, you have the return statement as last line of function
} else if (prop === "tracks") { // same exact condition of if above, stuff inside here will never execute
if (collection[id]["tracks"] !== "") { // always true, `collection[id]["tracks"]` is or undefined or an array - what do you want to check here?
collection[id]["tracks"].push("value") // if the array does not exist this will error out, careful there
}
return collection; // you can remove this line, you have the return statement as last line of function
} else if (collection[id]["prop"]["value"] === "") {
delete collection[id]["prop"]
}
return collection
// you are missing the closing }
function updateRecords(id, prop, value) {
if (prop === "tracks") {
if (collection[id].hasOwnProperty("tracks") === false) {
collection[id]["tracks"] /* I want to add "tracks" object if any of id object dont have it*/
}
} else if (prop === "tracks") { // same exact condition of if above, stuff inside here will never execute
if (collection[id]["tracks"] !== "") { // always true, `collection[id]["tracks"]` is or undefined or an array - what do you want to check here?
collection[id]["tracks"].push("value") // if the array does not exist this will error out, careful there
}
return collection; // you can remove this line, you have the return statement as last line of function
} else if (collection[id]["prop"]["value"] === "") {
delete collection[id]["prop"]
}
return collection
// you are missing the closing }
you are pushing to something that is not an array. that’s not better. that will give an error, push can be used only on existing arrays
also, collection[id][prop] = collection[id][prop] || collection[id][prop].push(value)
the returned value of push is a number, if you assigned the returned value to something, it will not be what you want collection[id]["tracks"] still does nothing
I really suggest you switch off your computer, take pen and paper, and try to think what needs to happen
this time, I will give a small suggestion, like the barebones of your pseudocode algorithm
given id, prop, value
if value is an empty string
[do something]
else
if prop has value of tracks
[do something]
else
[do something]
[collection must be returned at the end]