Record Collection code issue

Basic JavaScript: Record Collection 91/110

I am confused on why the code I wrote is not working for the challenge. I feel as if i’m getting the proper outputs even if it isn’t the best way to write it. Condition 2 and 3 are the ones that are not working. Can someone please tell me where I went wrong?

// 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];
  }
  else if (collection[id][prop] == undefined){   
    collection[id][prop] = value; //2,3,8
  }
  else if (collection[id][prop][value] == undefined){
    var myArray = collection[id][prop];
    myArray.push(value);
    collection[id][prop] = myArray; //5,6 
  }
  return collection
}

Also if you have tips on how to do the push function better please let me know, I had troubles using the push function on an object so I turned it into an array to use it.

Thank you!

If prop is “tracks”, this will not create an array.

Can you explain your reasoning here? I’m pretty sure that this isn’t doing what you want it to.

So first it checks if the prop is defined in the object.
collection[id][prop] == undefined
if it is defined it moves onto the section of code and asks if the value is defined in the object.
else if (collection[id][prop][value] == undefined)
If the value is undefined that means the value is not part of the prop.
When that happens I push the new value to the prop.

are you sure that’s how you check something inside an array or object?
if you want to check if 2 is inside array, you don’t check if array[2] is undefined.
why do you want to check if it is inside it?

you have not turned it in an object… collection[id]['tracks'] is an array, even if you do not assign it to myArray.
As such, collection[id][prop].push() will work fine, but only on already existing arrays

It seems you are missing how to differantate between when prop is tracks and when it isn’t

Okay I see a lot of flaws in this code and I went a different route and passed the lesson but just for knowledge sake I have adjusted this code and now I am only not passing on the point:
after updateRecords(5439, "artist", "ABBA") , artist should be "ABBA"

Yet when I console.log it, it seems to output it.

Here is the updated code:

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

  }
  else if (collection[id][prop][value] == undefined){
    var myArray = collection[id][prop];
    myArray.push(value);
    collection[id][prop] = myArray; //5,6 
  }
  return collection
}
console.log(updateRecords(5439, "artist", "ABBA"))

Sorry I should have changed this as well

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

  }
  else if (collection[id][prop][value] == undefined){
   collection[id][prop].push(value);
  }
  return collection;
}
console.log(updateRecords(5439, "artist", "ABBA"))

this line is still wrong on its own
value will never be a value that could be an index of an array or a property name of an object