(Basic JavaScript: Record Collection). Why isn't it working?

Hi guys! this is the error that the console displays to me :

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

But actually my code seems to do those things. Can you help me? please

function updateRecords(id, prop, value) {

  var selector = collection[id];
  var name = [2548, 2468, 1245, 5439];

  for (var i = 3 ; i >= 0; i--) {if (collection[name[i]].album == "") {delete collection[name[i]].album }}
  for (var i = 3 ; i >= 0; i--) {if (collection[name[i]].artist == "") {delete collection[name[i]].artist }}
  for (var i = 3 ; i >= 0; i--) {if (collection[name[i]].tracks == "") {delete collection[name[i]].tracks }}

  if (collection.hasOwnProperty(id) == false || value == "") { console.log('invalid id or value'); }
  else { switch (prop) {
             case "album":
             if (selector.hasOwnProperty(prop) == false) { selector.album = value; }
             else if (selector.album.hasOwnProperty(value) == false) { selector.album.push(value); };
             break;
           case "artist": 
             if (selector.hasOwnProperty(prop) == false) { selector.artist = value; }
             else if (selector.artist.hasOwnProperty(value) == false) { selector.artist.push(value); };
             break;
           case "tracks" :
             if (selector.hasOwnProperty(prop) == false) { selector.tracks = [value]; }
             else if (selector.tracks.hasOwnProperty(value) == false) { selector.tracks.push(value); };
             break;
           default : console.log("this property isn't valid");
    }
  }

  console.log(collection);
  return collection;  
}

Firstly you’re hardcoding things. If I add a million more albums to the collection, the function should still work fine. Yours will break if it’s not exactly the same.

That won’t break things (although it defeats the.point of programming).

What breaks things is that you’re doing a lot of things wrong. The loops are hardcoding and don’t make sense anyway, they don’t do anything. I assume you were trying to fulfil the part of the task where it says delete if the value is empty. But you don’t even check that in the loops. Instead, you console log if the value is an empty string.

Then you try to push for everything. push is an array method. “artist” and “album” are just strings, you can’t use push. And you can use it for “tracks”, but if tracks is not there initially, then you need to create the array first.

I would look at trying this again

Thank you! Now the code seems to work properly

function updateRecords(id, prop, value) {

  var selector = collection[id]; 
  
  if (value != '') {
     switch (prop) {
           case "album":
           case "artist": 
               if (selector.hasOwnProperty(prop) == false) { selector[prop] = value; }
               else if (selector.artist.hasOwnProperty(value) == false) { selector[prop][value]; }
             break;
           case "tracks" :
               if (selector.hasOwnProperty(prop) == false) { selector.tracks = [value]; }
               else if (selector.tracks.hasOwnProperty(value) == false) { selector.tracks.push(value); }
             break;
           default : console.log("this property isn't valid");
    }
  } else { delete selector[prop]}

  console.log(collection);
  return collection;  
}

this doesn’t do anything… selector[prop][value] like that is not doing anything, and selector.artist can’t have a property because its a string
plus, if I wanted to update the records like this, the function wouldn’t work: updateRecords(5439, "release_year", 1978)

Remember that selector is an object. The object has the properties to check. As @ilenia has mentioned, selector.artist is a string value (not an object), so you can call the hasOwnProperty method on it.

@ilenia Thanks to have report to me the mistakes.
I think that thanks to both of you @ilenia @RandellDawson I have understand what I did wrong . Thank you guys!

@ilenia for the updateRecords(5439, "release_year", 1978) I deliberately decided to not add others prop but i would ask you if this will be work

function updateRecords(id, prop, value) {

  var selector = collection[id]; 
  
  if (value != '') {
     switch (prop) {
           case "tracks" :
               if (selector.hasOwnProperty(prop) == false) { selector.tracks = [value]; }
               else if (selector.hasOwnProperty(prop) == true) { selector.tracks.push(value); }
             break;
           default : if (selector.hasOwnProperty(prop) == false) { selector[prop] = value; };
    }
  } else { delete selector[prop]}

  console.log(collection);
  return collection;  
}

and what if you need to update an already existing property?
like the object has artist: "abba" and you want to set it artist: "ABBA", would your function be able to manage that?


this is still wrong… value is not a property, selector.tracks is an array, not the object
why don’t you just do an if/else?

I think you saw the second mistake in the previous code

Instead for the first one i think i can simply delete the if statement :
(I also tried to clean up the 'tracks' part of the switch)

function updateRecords(id, prop, value) {

  var selector = collection[id]; 
  
  if (value != '') {
     switch (prop) {
           case "tracks" :
               if (selector.hasOwnProperty(prop) == true) { selector.tracks.push(value); }
               else { selector.tracks = [value]; }
             break;
           default : selector[prop] = value;
    }
  } else { delete selector[prop]}

  console.log(collection);
  return collection;  
}

Regarding the if/else Having to do a different case for 'tracks' i decided to use switch()