skip to Main Content

We have this Javascript object:

const recordCollection = {
  2468: {
    albumTitle: '1999',
    artist: 'Prince',
    tracks: ['1999', 'Little Red Corvette']
  },
  1245: {
    artist: 'Robert Palmer',
    tracks: []
  },
  5439: {
    albumTitle: 'ABBA Gold'
  }
};

To be able to update the properties of the object, one of the two functions could be used:

unction updateRecords(records, id, prop, value) {
  if (value === "") {
    delete records[id][prop];
  } else if (prop !== "tracks" && value !== "") {
    records[id][prop] = value;
  } else if (prop === "tracks" && value !== "") {
    if (records[id].hasOwnProperty("tracks") === false) {
      records[id][prop] = [];
    }
    records[id][prop].push(value);
  }
  return records;
}

let result = updateRecords(recordCollection, 2468, "tracks", "");
console.log(result);

OR

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

let result = updateRecords(recordCollection, 2468, "tracks", "lala");
console.log(result);

Within the function, there’s an initial check to see if a value is an empty string using ‘ if (value === "") ‘. Subsequent to this, two other conditions are examined, with the empty string condition consistently verified using the ‘&&’ operator.

Is this repeated check for an empty string necessary? Can one safely remove the ‘&& value !== ""’ part without introducing bugs or altering the intended functionality of the code?

If with and without are the same, which code would be better and why?

Please provide an explanation for your answer (whys and hows..). Thank you a lot!

I have already tried both versions with douple checking and without, but couldn’t see a difference. This code is a solution for a Javascript exercise from freecodecamp. I have the specific questions I already mentioned, but any additional information is also welcomed! Thanks!

2

Answers


  1. Assuming the first if (value === "") evaluates to false, the subsequent else ifs don’t need to test if value !== "" because you can assume that to be the case by definition. So your first function can be simplified to:

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

    I would argue that this version of the code is better because it is shorter and less redundant.

    Login or Signup to reply.
  2. You don’t need to repeat opposite if conditions in if else since if else isn’t executed if if is.

    If you don’t mind using 2 conditional operators:

    const updateRecords = (records, id, prop, value) =>
      value ? 
        prop === 'tracks' ? (records[id].tracks ??= []).push(value) : records[id][prop] = value : 
        delete records[id][prop];
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search