title: How does the original implementation handle undefined and null elements, and why is it a problem? How does your code solve that?
body: <pre><code>function isEmpty(value) {
    let type = typeof value;
    if (type === 'object') {
      return Object.keys(value).length === 0;
    } else if (type === 'string') {
      return value.length === 0;
    }
  }
  // current test cases
  isEmpty({});                  // true
  isEmpty({ name: 'Janice' });  // false
  
  isEmpty('');                  // true
  isEmpty('Janice');            // false
  
  isEmpty([]);                  // true
  isEmpty(['Janice']);          // false
  </code></pre>
  <p>The following function is used in an application to determine whether various values are considered &quot;empty.&quot;The current implementation only handles strings and objects.</p>
  <p>We now need to modify it to handle arrays a little better. Since arrays are also objects, this code seems to handle arrays well. In fact, it works with most arrays, but that's almost by accident. You may encounter subtle problems in which the function returns false for arrays that have no elements.</p>
  <p>Given this information, why does the function work in most cases? Describe a situation where it may return false when you're expecting it to return true. Modify isEmpty so that it explicitly checks whether an array is empty in accordance with your expectations. Your modified code should clearly distinguish between arrays and other objects. Be sure to test your code to prove that it works as expected. Include your test case(s) in your answer.</p>
  <p>My implementation so far:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function isEmpty(value) {
    let type = typeof value;
    if(value === null || undefined){
      return value.length === 0;
    }
    if(Array.isArray(value)){
        return value.length === 0;
    }
    if (type === 'object') {
      return Object.keys(value).length === 0;
    } else if (type === 'string') {
      return value.length === 0;
    }
  }
  
  // current test cases
  console.log('{}', isEmpty({}));                  // true
  console.log("{ name: 'Janice' }", isEmpty({ name: 'Janice' }));  // false
  
  console.log("''", isEmpty(''));                  // true
  console.log("'Janice'", isEmpty('Janice'));            // false
  
  console.log('[]', isEmpty([]));                  // true
  console.log("['Janice']", isEmpty(['Janice']));          // false
  
  console.log('[null]', isEmpty([null]));              // false
  console.log('[undefined]', isEmpty([undefined]));         // false
  
  console.log('{undefined}', isEmpty({undefined}));         // false
  console.log("{null: 'true'}", isEmpty({null: 'true'}));      // false
  console.log("'null'", isEmpty('null'));
  console.log("'undefined'", isEmpty('undefined'));</code></pre>
  </div>
  </div>
  
  My answer:
  The typeof operator also considers arrays as objects so this function will handle arrays as objects as well, a situation where it may return false where it is expected to return true is when null or undefined are elements in the array or object being evaluated.</p>