HowProgrammingWorks / HowProgrammingWorks/DataTypes

Null type checks is not considered by the tests length

Open
#63 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
39
Forks
228
PR merge metrics
No merged PRs in 30d

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading 4-count-types.js and the associated tests to understand how countTypesInArray is currently constrained to 200 characters. Decide from the issue discussion whether null should be counted explicitly or whether the test expectation should change, then verify that the chosen behavior is covered without violating the exercise’s length requirement.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.