Basic JavaScript: Record Collection; I need help please!

any suggestions guys?

function updateRecords(id, prop, value) {
  switch (id, prop, value) {
    case (prop !== "tracks" && value !== ""):
    collection[id].prop = value;
    break;
    case (prop === "tracks" && !collection.hasOwnProperty("tracks")):
    collection[id].prop = [];
    collection[id].prop.push(value);
    break;
    case (prop === "tracks" && value !== ""):
    collection[id].prop.push(value);
    break;
    case (value === ""):
    delete collection[id].prop;
    break;
  }
  
  return collection;
}

switch takes a single expression or single value. You have supplied 3 values. Only the last value/expression in the series of values gets evaluated by the cases. The cases’ values should match the expression or value specified by the switch statement in order for the case code block to execute.

All of your case values are Boolean values, Since only value gets evaluated, none of your cases values (true or false) will match, so nothing changes in the collection which gets returned.

I suggest reading more about the switch statement.

Okay, thanks for explaining that, but before using switch I tried with if and else if statements using the same mentioned cases and still have some problem with my code…Could you comment on the cases I wrote in my code?

Can you show your the code you tried with the if/else statements just to make it clear exactly what you tried?

function updateRecords(id, prop, value) {
  if (prop !== "tracks" && value !== ""){
    collection[id].prop = value;
  }
  else if (prop === "tracks" && !collection.hasOwnProperty("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;
  }
  
  
  return collection;
}

the objects never have a property called prop

remember what’s the main difference between dot notation and bracket notation


after you solve that, there is a small logic issue for an edge case… what happens with updateRecords(2456, "tracks", "")? (consider that the 2456 is a valid id)

Yeah, I got that and this fixes some errors but still show these 2 errors:

// 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
function updateRecords(id, prop, value) {
  if (prop !== "tracks" && value !== ""){
    collection[id][prop] = value;
  }
  else if (prop === "tracks" && !collection.hasOwnProperty("tracks")){
    collection[id].tracks = [];
    collection[id].tracks.push(value);
  }
  else if (prop === "tracks" && value !== ""){
    collection[id].tracks.push(value);
  }
  else if (value === ""){
    delete collection[id][prop];
  }
  
  
  return collection;
}

this always evaluates to false

this still evaluates to false

Sorry for this mistake.
It is “tracks” not “value”.
But I didn’t get you, I think it should always evaluate to false since it is the case where the album doesn’t have a “tracks” property!!!

the issue is that it evaluates to false even if the object you want to check has the tracks property

what’s the object you want to check? what’s the object you are checking with hasOwnProperty()?

I deal with every if and (else if) statement as completely independent case just like switch statement, and not like if and (else) statements… Is this right?

I think I got the point I just added [id] to to check with hasOwnProperty() and solved the problem. But still need answer to my question I asked you.

they are exclusionary, only one of the statements in an if/else if/else chain is executed

now, if you had an item like

1199: {
   album: "Night"
}

and nothing else, and the function call is updateRecords(1199, "tracks", ""), what would happen here?

it is an edge case, but you can solve it easily, actually, and that would also let you make many conditions of the statements simpler

Now everything is clear, thank you very much. :+1: :star_struck: