Record Collection (JS knows everything? :D)

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.

Challenge: Record Collection

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/record-collection

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

    if (collection[id].hasOwnProperty("tracks")){
      }else{
        collection[id]["tracks"];
      }else if (collection[id]["tracks"] !=== "")){
        collection[id]["tracks"].push("value")
      }else if (collection[id]["prop"]["value"] === "")){
        delete collection[id]["prop"]
      }
        

  return collection;
}

It shows I messed up something

could you post the whole function? it is difficult to know what is just been not pasted here and what is not there

function updateRecords(id, prop, value) {
if (collection[id].hasOwnProperty("tracks")){
      }else{
        collection[id]["tracks"];
      }else if (collection[id]["tracks"] !=== "")){
        collection[id]["tracks"].push("value")
      }else if (collection[id]["prop"]["value"] === "")){
        delete collection[id]["prop"]
      }
        

  return collection;
}
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 }

then create an empty array and assign it there

remember that if you have a chain of if/else if statements only one of them will execute. You need to rethink your logic flow

Now I created this function but it still not working:

function updateRecords(id, prop, value) {
    if (prop === "tracks" && collection[id].hasOwnProperty("tracks") == false){
      collection[id][tracks]
    }else if (prop === "tracks" && value !== ""){
      collection[id][tracks].push(value)
    }else if (value == ""){
      delete collection[id][prop]
    }
 

  return collection;
}

Don’t have any idea how to write it properly :disappointed: :disappointed:

  1. you don’t have a variable called tracks so you can’t write [tracks], it needs to be ["tracks"]
  2. collection[id]["tracks"] is still doing nothing - how do you assign a value to a property?

the other thing you are missing is what to do when prop is not tracks.

function updateRecords(id, prop, value) {
      while(prop === "tracks"){
        if (collection[id].hasOwnProperty("tracks") && collection[id]["tracks"] !== ""){
            collection[id]["tracks"].push(value)
        }else if (collection[id].hasOwnProperty("tracks") == false){
            collection[id]["tracks"]
        }
        }else if (prop !== "tracks" && collection[id][prop] !== ""){
        collection[id][prop] = collection[id][prop] || collection[id][prop].push(value)
      }else if (value === ""){
        delete collection[id][prop]
      }
}

Is this better?

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]

Okay, it helped. Thanks

function updateRecords(id, prop, value) {;
    if (prop === "tracks"){
      if (value === ""){
        delete collection[id][prop]
      }
      collection[id][prop] = collection[id][prop] || []
      
      collection[id][prop].push(value)
    }else if (value !== ""){
      collection[id][prop] = value
    }
    return collection
}

I think now is better than in challenge. Instead of deleting every prop when value is (""), it only deletes “tracks” when we pass empty value

and what if I want to delete the artist property?

updateRecords(2548, "artist", "")

you should be able to delete any kind of property

there is even a test for that:

After updateRecords(2548, "artist", "") , artist should not be set

I don’t see how you can pass this