frontend-challenges icon indicating copy to clipboard operation
frontend-challenges copied to clipboard

13 - IsEmpty

Open jsartisan opened this issue 1 year ago • 1 comments

index.js

export function isEmpty(value) {
  switch (true) {
    case Array.isArray(value) && value.length > 0:
      return false;
    case value instanceof Map && value.size > 0:
      return false;
    case value instanceof Set && value.size > 0:
      return false;
    case typeof value === "object" && value !== null && Object.keys(value).length > 0:
      return false;
    default:
      return true;
  }
}

jsartisan avatar Feb 24 '24 06:02 jsartisan

The questions needs you to understand how to check certain data types in javascript:

  1. Array can be checked with Array.isArray(value).
  2. Map and Set types can checked with instanceof.
  3. We can check for object with type typeof === "object". The main caveat is that null values are also object in javascript. So we need to put additional check for null values.

jsartisan avatar Feb 24 '24 06:02 jsartisan