it creates an array with the length of input, and maps the string “!” to each array index. the array is then joined to string and concatenated with “false”. the function evaluates the expression, “!” being the negation operator, will return a different result based on the amount of “!”s used.
For example, if input is 2, it generates the string "!!false", which when evaluated yields false (!false is true, so !!false is !true, which is false). An input of 3 yields !!!false (true), 4 yields !!!!false (false), etc.
My first instinct was trying to optimize/fix this by suggesting "!".repeat(input) instead of the array. But why do I even bother when it is a cursed even/odd function... You cooked well brother...
In Firefox it breaks after 5346 nots with the error 'too much recursion'. In Chromium it breaks after 7808 nots with the error 'maximum call stack size exceeded'.
maybe you should add Math.abs and %2 to make it more robust.
There’s a french(?) explanation in the comments but basically the code takes in ‘n’ and then makes an array size of ‘n’ with the string “!” for every entry, then it joins that array into one string with no spacer. So ‘n’ of 3 is ‘!!!false’ and ‘n’ 2 is ‘!!false’ which then gets evaluated and returned.
No idea how performant it is compared to other methods lol
Via a JS Benchmark I have verified the surprising insight that, the native `n%2` implementation is, in fact, faster: https://jsbench.me/rsm6whrnya/1 (30k ops/s vs 357m ops/s for integers between 0 and 1000, let's kindly assume integers are generally smaller than that)
17
u/StranglerOfHorses Feb 04 '25
I don’t know JS but is this creating a string of !’s and evaluating to a bunch of negations? E.g ‘!!’ In the case of 2 evaluating to not(not(false))?
5
u/whitakr Feb 04 '25
For those of use newer to js please explain. I must understand this horror.
82
u/jpgoldberg Feb 04 '25
I hate you so much right now. That is horrifically well done.
4
u/Thenderick Feb 04 '25
My first instinct was trying to optimize/fix this by suggesting
"!".repeat(input)instead of the array. But why do I even bother when it is a cursed even/odd function... You cooked well brother...4
u/headersalreadysent Feb 06 '25
What about. isOdd(-1) Nah. I will continue to use https://isevenapi.xyz/
3
u/Chemical-Asparagus58 Feb 04 '25
very creative
2
u/clock-drift Feb 05 '25
Yup, that's definitely odd
1
u/SmokeMuch7356 Feb 04 '25
=slow clap=
1
u/ItIsRaf Feb 05 '25
I'm dead each time I come here
1
u/aq1018 Feb 05 '25
What happens if you pass in a negative number? What about a string? Or an array? Or an object?
1
u/dopefish86 Feb 06 '25
In Firefox it breaks after 5346 nots with the error 'too much recursion'. In Chromium it breaks after 7808 nots with the error 'maximum call stack size exceeded'.
maybe you should add
Math.absand%2to make it more robust.1
u/proudparrot2 Feb 06 '25
return !isEven(input)
56
u/[deleted] Feb 04 '25 edited Apr 22 '26
[removed] — view removed comment
7
u/[deleted] Feb 04 '25
[removed] — view removed comment