Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
Sadly JavaScript doesn't use integers, so this solution will fail if you give it a value of
0.12
.Test cases was fixed a year ago.
Thank you.
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
If you pass 0.5 to the function, it won't return the negative value;
I'll try :)
I wish the functions and parameters were named more clearly, because the solution has beauty.
Anyway, it says:
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 totrue
and any undefined value tofalse
.When adding or subtracting with
true
orfalse
values,true
is coerced to the value1
andfalse
is coerced to the value0
.So what's going on is that I'm mapping the value in the
tokens
array to a value of1
or0
.In regard to the process of
tokens = exp.exec(a)
, every RegExp object in Javascript has a property called thelastIndex
. When you use theexec
method of a RegExp object that uses a global (g
) modifier, thelastIndex
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 theexec
method again it will start from the position specified bylastIndex
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.Can you break down the process of 'tokens = exp.exec(a)' and '!!tokens[i + 1]'?
Thanks!
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), andtokens[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 againsta
.The expression
value - !!tokens[i + 1]
decrements the value in the precedence array if there's a corresponding match againstb
.Can someone offer a detailed/step by step explanation?
Explanation for within the map functions?
Won't that have inconsistent behavior? You're using absolute equality in the first comparison but not in the second comparison (=== and <=). If someone passes a string "1" they'll get 5, but if they pass "0" they'll get 5 too. === should be changed to ==.
This comment is hidden because it contains spoiler information about the solution
AHH. Oops. Commented on wrong solution.
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 :)
Loading more items...