function set(object, path, value) {
  // write your code below

  const isObjectInvalid = !object || typeof object !== 'object';

  // escape condition
  // return null if object/path is invalid
  if (isObjectInvalid || !path) {
    return null;
  }

  // cleaning the path if type is string
  if (typeof path === 'string') {
    path = path.replaceAll('[', '.');
    path = path.replaceAll(']', '.');
    path = path.split('.');
  }

  // filtering out empty values
  path = path.filter(Boolean);

  let i;
  let currentKey;
  let currentItem = object;

  for (i = 0; i < path.length; i++) {
    currentKey = path[i];

    // handling missing portion of the object case
    if (!Object.prototype.hasOwnProperty.call(currentItem, currentKey)) {
      // 'x' => isNaN('x' && Number('x')) => isNaN('x' && NaN) => isNaN(NaN) => true
      // '0' => isNaN('0' && Number('0')) => isNaN('0' && 0) => isNaN(0) => false
      const isNonArrayMissingIndex = isNaN(path[i + 1] && Number(path[i + 1]));

      // creating object for all missing keys
      if (isNonArrayMissingIndex) {
        currentItem[currentKey] = {};
      } else {
        // handling array missing index case
        currentItem[currentKey] = [];
      }
    }

    // if the last key then set the value
    if (i === path.length - 1) {
      currentItem[currentKey] = value;
    } else {
      // updating currentItem
      currentItem = currentItem[currentKey];
    }
  }

  // return the original object
  return object;
}
Read-only