Skip to content

Null type checks is not considered by the tests length #63

@ghost

Description

There is a problem with countTypesInArray function (4-count-types.js). The tests require a solution to be 200 signs max, but in this case, we kinda lose the check for nulls.

As you know null in JavaScript has a type of object. Thus, the implementation you require will count objects but not nulls. So, the function is not suitable for checking all the JS types.

I clearly understand that such a check can be done using a ternary operator but it's not convenient and intuitive enough.

The implementation below considers this JS's quirk:

const data = [
  false,
  'a',
  22,
  'hey',
  undefined,
  42,
  12,
  true,
  { a: 12 },
  { name: 'Jack' },
  'foo',
  'bar',
  true,
  null,
  undefined,
  Symbol('a'),
  null
];

const countTypesInArray = data => {
  const h = {};
  for (const item of data) {
    if (typeof item === 'object' && item === null) {
      h['null'] = 1;

      if (h['null'] > 0) {
        h['null']++;
      }
    } else if (typeof item in h) {
      h[typeof item]++;
    } else {
      h[typeof item] = 1;
    }
  }
  return h;
};

console.log(countTypesInArray(data));

So, maybe you should consider an option for making tests a little bit more loose for null checks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions