Ad
  • Custom User Avatar

    I'll try :)
    I wish the functions and parameters were named more clearly, because the solution has beauty.

    Anyway, it says:

    I'm d. Given a regex I'll return an Int score of how many matches I find in b - a. A negative number would mean b has more.

    Next I'll return. For each pattern #, ., and plainWords, see if we get a truthy count when we diff(b-a).
    If we get that count there is a difference. We can short-circuit the rest of the logical || checks and return based on diff < 0.
    Otherwise keep looking, and if at the end they were all the same, they must be equal (no difference less than 0), so return a.

  • Default User Avatar

    The expression tokens[i + 1] stores the value of the regular expression's i_th_ captured string, or undefined if the i_th_ captured string doesn't exist.

    The value of any matched string will always be truthy because it's not an empty string.
    The value of undefined will always be falsy.

    The expression !!tokens[i + 1] therefore coerces any matched string value to true and any undefined value to false.

    When adding or subtracting with true or false values, true is coerced to the value 1 and false is coerced to the value 0.

    So what's going on is that I'm mapping the value in the tokens array to a value of 1 or 0.

    In regard to the process of tokens = exp.exec(a), every RegExp object in Javascript has a property called the lastIndex. When you use the exec method of a RegExp object that uses a global (g) modifier, the lastIndex property is updated to the index of the position in the string that ends the match, and resets to 0 when there is no further match. When you run the exec method again it will start from the position specified by lastIndex rather than from the beginning of the string. It's similar to the behavior commonly associated with the sticky (y) modifier on regular expressions, but with better backwards-compatibility in Javascript.

  • Custom User Avatar

    Can you break down the process of 'tokens = exp.exec(a)' and '!!tokens[i + 1]'?

    Thanks!

  • Default User Avatar

    The regular expression parsing the selector has 3 capture groups, in order of precedence. These end up populating tokens[1] (if there is a # match), tokens[2] (if there is a . match), and tokens[3] (if there is an element match).

    !!tokens[i + 1] will translate to 1 if a match exists, 0 if a match does not exist.

    The expression value + !!tokens[i + 1] increments the value in the precedence array if there's a corresponding match against a.

    The expression value - !!tokens[i + 1] decrements the value in the precedence array if there's a corresponding match against b.

  • Custom User Avatar

    merp.length/2 will return a floating point value if merp.length is odd.

    Use Math.floor() or Math.ceil() to prevent using a floating point value as an array index

    Hope that helps :)