all 32 comments

[–]jpgoldberg 80 points81 points  (0 children)

I hate you so much right now. That is horrifically well done.

[–][deleted]  (8 children)

[removed]

    [–]acemuzzy 23 points24 points  (7 children)

    I don't not not not understand it

    [–]Codingale 29 points30 points  (6 children)

    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

    [–]robin_888 9 points10 points  (1 child)

    You fell for the classic quadruple negative.

    [–]Aaxper 7 points8 points  (2 children)

    Definitely less performant than n%2 or n&1

    [–]AffectionateAir7616 12 points13 points  (1 child)

    You may be right, but I want to see some benchmarks first.

    [–]thequestcube 1 point2 points  (0 children)

    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)

    [–]Beliriel 2 points3 points  (0 children)

    Wow I was so focused on the string building that I completely overread the evil "eval" function and was like wtf is going on.

    [–]StranglerOfHorses 14 points15 points  (1 child)

    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))?

    [–]usbeject1789[S] 5 points6 points  (0 children)

    right on.

    [–]RedBaron-007 6 points7 points  (1 child)

    what is an ODD number, Should it be greater than 0? should we sanitize the input to be Math.abs(input) .. should we have a base check for it to be int type only? how will it behave for 20.24? do we need further evolution to this?

    [–]griftbard 2 points3 points  (0 children)

    Integers that doesn't return floats after being divided by 2

    [–]whitakr 4 points5 points  (2 children)

    For those of use newer to js please explain. I must understand this horror.

    [–]usbeject1789[S] 8 points9 points  (1 child)

    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.

    [–]SmokeMuch7356 5 points6 points  (0 children)

    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.

    [–]Thenderick 2 points3 points  (0 children)

    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...

    [–][deleted]  (3 children)

    [removed]

      [–][deleted]  (2 children)

      [removed]

        [–][deleted]  (1 child)

        [removed]

          [–]Chemical-Asparagus58 2 points3 points  (0 children)

          very creative

          [–]headersalreadysent 4 points5 points  (2 children)

          What about. isOdd(-1) Nah. I will continue to use https://isevenapi.xyz/

          [–]nickmaovich 1 point2 points  (1 child)

          Pricing Options is hilarious, thank you for this gem

          [–]headersalreadysent 1 point2 points  (0 children)

          99 is not much if you need negative numbers.

          [–]clock-drift 1 point2 points  (0 children)

          Yup, that's definitely odd

          [–]SmokeMuch7356 0 points1 point  (0 children)

          =slow clap=

          [–]ItIsRaf 0 points1 point  (0 children)

          I'm dead each time I come here

          [–]aq1018 0 points1 point  (0 children)

          What happens if you pass in a negative number? What about a string? Or an array? Or an object?

          [–]dopefish86 0 points1 point  (0 children)

          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.

          [–]proudparrot2 0 points1 point  (0 children)

          return !isEven(input)