Insight on specific order of if statements in Record Collection solution

Insight on specific order of if statements in Record Collection solution
0.0 0

#1

I initially had the “if value” and “if prop” statements in reverse order as they were stated that way in the instructions, but it only works when they are in the order seen in the code below. I don’t understand this. insight appreciated.

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

#2

Try focusing on what each if/else clause is doing to fully understand the code. Taking out the inner if/else will make it easier to understand.

function updateRecords(id, prop, value) {
  if (value !== ""){
   // value is not empty. Process request
  }else{
    // value is empty. Delete the item from the collection, per spec.
    delete collection[id][prop];
  }
  return collection;
}

If value is empty, we don’t really care what the prop is because it’s going to be deleted regardless. So, if you’re checking props !== "tracks" first, you’re changing the way the logic flows in the program.