Hacktoberfest - first 5 PR - part 3

3rd PR -  evalOrQuery

The third issue is to check the similarity of a given result and a given query. Description from the issue:
  • Return an array of objects that follow the criteria specified in the query.
  • Query contains certain values mapped to operators '<', '>' and '='.
  • Any objects in the array that obey this criteria when compared using the comparator should be returned.
  • Comparator is a function that expects an object from the array and value to be compared with (from query) and returns -1, 0, or 1 informing whether the object (1st argument) was less than, equal to or greater than the value (2nd argument).

Understand The Problem 

The requirements may look quite hard to understand, so just take a look at the arguments, where we have three args: arr, query and comparator
The query is an object containing three comparisons: greater than, less than and equal as the properties, and for each value, we will put it into the comparator and do the comparison.

So lets say we have:
  • query['<'] = 13
Then we should check if this condition happens:
  • comparator(13) = -1 //because less than in number is -1

The Solution

Now we already know the concept, these are the steps in order to be done:
  1. Create a constant object Q which pairs the comparison symbols to its number sign:
    • <: -1
    • =: 0
    • <: 1
  2. Check the validity of arguments
  3. Create an empty array rt to be return
  4. Process all elements in arr
So the final code would look like this:


Link:

Comments

Popular Posts