• frist (unregistered)

    min(frist)

  • RobyMcAndrew (unregistered)

    What incompetence! Everyone knows the best way to swap two numbers is to use three XORS.

  • Unicorn (unregistered)

    I've found an easter egg on the post. Try clicking on the word "pest".

  • Damien (unregistered)

    I'm not sure how Phlogiston theory and Relativity can even be compared. Are you sure you're not thinking of luminiferous aether?

  • Steve_The_Cynic (nodebb) in reply to Damien

    Are you sure that isn't part of the joke?

  • Steve_The_Cynic (nodebb) in reply to Unicorn

    It's a Remy post. That stuff's normal.

  • isthisunique (unregistered)

    is to use three XORS

    That's not always reliable in JS although would probably work fine here, as like as the input types can be juggled into an accurate int type.

    The funniest solution for this problem I have seen is: (...args) => args[0] === args.sort()[1]

    It works quite well all things considered, if you can figure out how to use it and what it's meant to do.

  • my name is missing (unregistered)

    In Phlogiston theory min and max are the same so the code is fine.

  • Carl Witthoft (google)

    Considering someone just posted an interview question about JS to one of the StackExchange sites, to wit:

    Can the following ever evaluate to TRUE if (a ==1&& a==2&&a==3)

    And people posted two completely different, allowable, valid solutions, I think I don't want ever to learn JS. (FWIW, I would have gone with a third, valid solution of operator overloading) ref: https://stackoverflow.com/questions/48270127

  • Marcus (unregistered)

    Remy, I'm sorry you couldn't find better friends.

    regards,

    Marcus PS: My last name was Phlogistein, you little twerp.

  • TruthNerds (google) in reply to RobyMcAndrew

    Great idea!

    min = min ^ Math.max(min,max); max = max ^ Math.min(min,max); min = min ^ Math.max(min,max);

  • Messerschmitter (unregistered) in reply to Carl Witthoft

    I think any language that supports property getters that can run code can do this, not only Javascript.

  • ImAnIdiot (unregistered)

    Apparently I'm an idiot but how does: min = Math.min(min, max); max = Math.max(min, max); set both to equal to the lowest of the two?

  • ImAnIdiot (unregistered)

    just reread it - never mind... I am an idiot

  • Jeremy Hannon (google) in reply to Messerschmitter

    Property getters that run code can easily be abused, but I wouldn't say it is wrong in all cases. Typically I use them in an object to lazy-load an item that much of the time is never used and only needs to be there if it does get used - so the getter will initialize its backing property if it hasn't already been done. Of course, this is typically only done with read-only properties. (.Net by the way)

  • fragile (unregistered)

    Something about assertions in source never felt right. Better to filter.

    lodash has an xor

  • Sole Purpose of Visit (unregistered)

    OK. I'm stupid. I couldn't spot why the min/max thing does what Remy says it does, until I realised that for no very good reason the code treats the function parameters as sacrosanct in some way, and buggers everything up by reusing the variable names. This is of course sick and silly.

    Which would lead the average minimally talented ape to realise that neither integer, on the way in, is constrained to be either a max or a min. I'm quite surprised that they didn't include checks for one of them being an arithmetic mean, a geometric mean, or even a median. (God knows how that would work, but it couldn't be less awful.)

    I'm getting old. So very old. I am increasingly drawn to languages like Haskell, which I basically do not understand at all, but which puts a huge emphasis on describing the contract of the function by actually giving the parameters a proper type, or type alias. Passing anything sane like, say, range in this case would obviate absolutely all the rest of the rubbish. Fifty thousand, pah.

    (And yes, I am aware that you can do this in either JavaScript or PHP. But apparently you are trained to do the stupid thing instead.)

  • I am a robot (unregistered) in reply to RobyMcAndrew

    Or add and subtract considering values are numbers.

  • Erik Gern (unregistered)

    Sole Purpose of Visit, comment held for moderation seems to hate you.

  • MacFrog (unregistered) in reply to Carl Witthoft

    The really important question is: Does that expression ever evaluate to FileNotFound?

  • Pedro (unregistered) in reply to ImAnIdiot

    I still don't see the problem with the min/max swapping thing. It looks OK to me. Anyone care to explain why the swap wouldn't work ?

  • 🤷 (unregistered)

    For anyone who like me had a hard time figuring out why this code sets both variables to the lower of the two if min>max.

    1. Let's call this function like this
    checkPriceRangeTo(10,20,5)

    Now let's see what happens:

    min = Math.min(min,max);

    min is now set to 5, because the varialbe max is 5, min is 20.

    max = Math.max(min,max)

    Aha!Now we are doing this: max = Math.max(5,5). Which (very unsurprisingly, even for JavaScript) is 5. Congratulations, both variables now hold the value of 5.

  • 🤷 (unregistered)

    "min is 20."

    This, of course, should've read "was 20".

Leave a comment on “The Least of the Max”

Log In or post as a guest

Replying to comment #492436:

« Return to Article