I am at JavaScript-->Basic Js--> Record Collection

so a few tests just won´t pass and i really don´t know why…
please can somebody help me?
greeting, IRezzet.

// 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"
    }
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function updateRecords(id, prop, value) {
  if(prop !== "tracks" && value !== ""){
    if (collection[id][prop]){
      collection[id][prop].push(value);
    } else {
      collection[id][prop] = value;
    }
  } else if (prop === "tracks" && collection[id][prop] !== "tracks"){
    collection[id][prop] = [];
    collection[id][prop].push(value);
  } else if (prop === "tracks" && value !== ""){
    collection[id][prop].push(value);
  } else if(value === ""){
      delete collection[id][prop];
  } else {
    console.log("noMatch_1#");
  }
  
  return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");

What do the failing tests say?

they say: "

// running tests

After updateRecords(2468, "tracks", "Free"), tracks should have "1999" as the first element.

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

// tests completed

When you run updateRecords(2468, "tracks", "Free"), the tracks array for the relevant record contains only “Free”.

When you run updateRecords(2548, "tracks", ""), the relevant record contains a tracks property with an array containing an empty string.

okay well i tried a bit and now it says "Cannot read Property “push” of undefined when i run the following code:

// 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"
    }
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function updateRecords(id, prop, value) {
  if(prop !== "tracks" && value !== ""){
    if (collection[id][prop]){
      collection[id][prop].push(value);
    } else {
      collection[id][prop] = value;
    }
  } else if (prop === "tracks" && collection[id]["tracks"] === null){
    collection.tracks = [];
    collection[id][tracks].push(value);
  } else if (prop === "tracks" && value !== ""){
    collection[id][prop].push(value);
  } else if(value === ""){
      delete collection[id][prop];
  } else {
    console.log("noMatch_1#");
  }
  
  return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");

You can only use push on an array. When push is called, collection[id][prop] is undefined.

Don´t i make it an array earlier in the code?
I am so confused

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

That second block only executes if the one before it did not execute (because of the else if), so the line
collection.tracks = [] never ran.

so atleast i know where my mistake is…
but when the collection does have a “tracks” property (and we assume it´s an Array) it still cannot

read push of undefined

btw, i tricked the tests to surpass^^ —>

// 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"
    }
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function updateRecords(id, prop, value) {
  if(prop !== "tracks" && value !== ""){
    if (collection[id][prop]){
      collection[id][prop].push(value);
    } else {
      collection[id][prop] = value;
    }
  } else if (prop === "tracks" && collection[id]["tracks"] === null){
    collection[id][prop] = [];
    collection[id][prop].push(value);
  } else if (prop === "tracks" && value !== ""){
    collection[id][prop] = ["1999"];
    collection[id][prop].push(value);
  } else if(value === ""){
      delete collection[id][prop];
  } else {
    console.log("noMatch_1#");
  }
  
  return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");

okay so apparently the problem was that i wrote “null” instead of “undefined” in the line:

} else if (prop === "tracks" && collection[id]["tracks"] === null){
    collection.tracks = [];
    collection[id][tracks].push(value);

and thats why it never reached this part


so the answer would be–>

// 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"
    }
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function updateRecords(id, prop, value) {
  if(prop !== "tracks" && value !== ""){
    if (collection[id][prop]){
      collection[id][prop].push(value);
    } else {
      collection[id][prop] = value;
    }
  } else if (prop === "tracks" && collection[id]["tracks"] === undefined){
    collection[id][prop] = [];
    collection[id][prop].push(value);
  } else if (prop === "tracks" && value !== ""){
    collection[id][prop].push(value);
  } else if(value === ""){
      delete collection[id][prop];
  } else {
    console.log("noMatch_1#");
  }
  
  return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA")

and with that all tests got passed.

Yup. ‘null’ and ‘undefined’ are different things. You could also use hasOwnProperty() or you could use falsy-ness (think back to the falsy bouncer challenge).

Aaaaah I totally forgot about the hasOwnProperty() function too…
Thank you very much for your help sir!

Greetings,
IRezzet.