jeudi 22 août 2019

Why is the freecodecamp solution better than my inelegent working solution?

I have been working through the Freecodecamp Basic Javascript course. During the Record Collection problem I found a working solution that does not match the official proposed solution and id quite like to know what i should be looking at to reach the different outcome.

My solution passed all their requirements but uses very exact conditions. It feels less elegant.

I tried posting this same question on the Freecodecamp forums but it was closed immediately without any communication as to why.

Can you tell me why their solution is better than my own?

My code:

// Only change code below this line
function updateRecords(id, prop, value) {

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

  else if (prop === "tracks" && !collection[id][prop]) {
      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 provided code:

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 (value !== "") {
    collection[id][prop] = value;
  } else {
    delete collection[id][prop];
  }

  return collection;
}

Aucun commentaire:

Enregistrer un commentaire