• Brian (unregistered)

    And of course today's RWTF is the broken website... might wanna get your certs fixed, guys. Fortunately falling back to an http URL still works.

  • John (unregistered)

    It's not the certificate, it seems to be the server configuration which is completely screwed up:

    root@fry:~# openssl s_client -connect thedailywtf.com:443
    CONNECTED(00000003)
    write:errno=104
    ---
    no peer certificate available
    ---
    No client certificate CA names sent
    ---
    SSL handshake has read 0 bytes and written 176 bytes
    Verification: OK
    ---
    New, (NONE), Cipher is (NONE)
    Secure Renegotiation IS NOT supported
    Compression: NONE
    Expansion: NONE
    No ALPN negotiated
    SSL-Session:
        Protocol  : TLSv1.2
        Cipher    : 0000
        Session-ID:
        Session-ID-ctx:
        Master-Key:
        PSK identity: None
        PSK identity hint: None
        SRP username: None
        Start Time: 1513254517
        Timeout   : 7200 (sec)
        Verify return code: 0 (ok)
        Extended master secret: no
    ---
    
  • Shoreline (nodebb)

    Part of the problem is that javascript doesn't come with an array factory (correct me if I'm wrong and they invented it recently).

    I guess we'd want something like Array.prototype.create(length).

  • dr memals (unregistered) in reply to Brian

    "https everywhere"!

    except thedailywtf

    The reCAPTCHA should really have two other options "I am a robot" and "I might be a robot"

  • Still Not Allowed to Name Things (unregistered) in reply to Shoreline

    You are correct, so this is not a WTF. The following works in ES2015 or higher: new Array(10).fill(1).map((n,i)=>n+i)

  • jack (unregistered) in reply to Shoreline

    It’s a bit awkward, but you can do: Array.from(Array(len))

  • Barry Margolin (google) in reply to Still Not Allowed to Name Things

    The .join(",").split() is probably explained by just trying to map over Array(10) not working, because .map() ignores the undefined elements that the Array(size) constructure uses. Until that .fill() method was added, this was probably the most succinct way to do this. You can find some similar code posted in this Stack Overflow question. https://stackoverflow.com/questions/3746725/create-a-javascript-array-containing-1-n

  • Steve_The_Cynic (nodebb)

    I believe the operation you are looking for is ι maxNumber. :demonic_smile:

  • Sole Purpose of Visit (unregistered)

    It is (theoretically) perfectly cromulent functional programming code.

    Of course, it has this weird hack in the middle with the join/split thing, which is definitely a wtf. And it doesn't start at 1: it starts at arguments[1], which is a point to consider.

    Now, I know nothing about Javascript (despite having used it all too often), but there's no particular objection to using the result of this as a lazily-evaluated array. (Well, there might be. It depends upon how efficient a Javascript interpreter might be at taking a slice in the middle, or whatever.) And since I know nothing about Javascript, the only question I have to ask is this: does Javascript do lazy evaluation in this case? Because, if not, you're going to be looking at a sand-timer or a spinning circle for a very long time.

    Now, there are clear theoretical objections to this code, as seen in isolation: there are almost certainly better ways to produce an "infinite" sequence of natural numbers (bounded by the processor word limit), and for any given application there are almost certainly better ways to obtain the subset of that sequence that you actually need. (The mind boggles if you actually need all 2^31 or 2^63 of them.) But I don't see any huge problem with the construct. It uses well-known Javascript libraries to produce an easy-to-think-about sequence. And it doesn't use recursion, which might avoid the join/split fudge but would replace it with a stack overflow.

    And best of all ... it's not PHP. It does what it says on the tin. It is predictable.

    I suspect somebody has copy-pastaed a snippet of Clojure or OCamL or Haskell and helpfully "adapted" it by side-stepping the odd Javascript glitch here and there.

  • Zenith (unregistered)

    The array constructor in JavaScript is pretty stupid. If you pass it one number, it creates an array of that length, but passing more than two does what's expected. That's an annoying quirk to track down when your list is generated on the backend and assigned on the frontend.

  • Ouch, my brain (unregistered) in reply to Barry Margolin

    I don't do much of any JavaScript and I thing I burst a blood vessel in my brain from that link.

  • Sole Purpose of Visit (unregistered)

    Since I am "under suspension," let me try again.

    The question is not, why would anybody want an array of natural numbers? This code provides one.

    The WTF question is, how would anybody use the result -- numbers?

    The fact that neither the submitter, nor his/her colleagues, stumbled at the first gate of figuring out what is going on is ... probably the basic WTF. Because educated programmers who cannot at least provide a good guess at what ends up in numbers are probably better off in a more remunerative and less reflective career, such as management consultancy.

  • Registered (unregistered)

    Are you all sure this is not something compiled to ES5 from actual source code in more hip variant?

  • isthisunique (unregistered)

    For performance reasons the array is declared at the length it needs to be first. That is undone though by the pointless join and split. That sucks even as a poor man's copy which isn't needed here.

    My guess is it used to be something else (maybe copy pasta) and someone either didn't know what they were doing, didn't care or overlooked removing some bits maybe after being distracted. I have these distractions in my code about every one thousand lines so I always try to proof read it. I have slips every 500 lines when typing and mousing on automatic thinking about stuff not looking at exactly what I am doing.

    It might also be malice. Add in deliberate performance drains you can easily fix later that you need to be kept around for. Spend three hours doing nothing, say investigating then just delete two lines.

    It could also be stupidstitious where someone had to do it for something like arguments not understanding why then always does it.

  • Dragnslcr (nodebb)

    It's the JavaScript equivalent of having a database table with one autoincrement/serial integer column.

  • isthisunique (unregistered)

    I see from above the actual reason is the issue with undefined (sparse arrays). So real WTF is person submitting not removing two seemingly needless lines and seeing what happens.

  • Richard (unregistered) in reply to Sole Purpose of Visit

    Possibly I'm reading it wrong but isnt "maximumNumber" just a variable, so could be for example 7. As opposed to literally the maximum possible number

  • siciac (unregistered) in reply to isthisunique

    TRWTF is no one on the board can fire up node or their browser's built in Javascript. Here you go:

    a = new Array(10);
    a[0] = undefined;
    a[1] = undefined;
    a.map(x=>3);
    [ 3, 3, , , , , , , ,  ];
    

    That's why the person did the join and split: those elements aren't just undefined, but non-iterable, just in case Javascript didn't have enough terms for "missing".

  • Sole Purpose of Visit (unregistered) in reply to Richard

    Whoops! You may have a point there. Lacking context (and specific knowledge of the language), I made an unwarranted assumption.

    Then again, this would still be problematical in any language that doesn't feature lazy evaluation, because there's no guard (as shown, sigh) against even plausible large maxima, say a million or so. If that is the case here, I maintain that TRWTF is the team's bizarre inability to understand what is going on, rather than the construct itself...

  • TehShrike (unregistered)

    Since passing a number to the JS constructor doesn't instantiate an array of empty elements you can iterate over like most other languages, "generate an array containing a range of integers" is practically the JS fizzbuzz.

    The author of the code in the post added their own little flair with the .join(), and then added a bit of obfuscation by doing

    .map(function(){return ++arguments[1]})

    instead of

    .map(function(value, index){return index + 1})

    Personally, with modern JS, I would favor

    [ ...new Array(n) ].map((value, index) => index + 1)

  • Rich (unregistered)

    Looking at how the respected JS library lodash implements this: https://github.com/lodash/lodash/blob/4.17.4/lodash.js#L3890 I'm guessing there really is no nice pre-ES2015 way to implement a seemingly simple thing.

  • Anonymous (unregistered)

    I dont use JS but what is wrong with just using for loop to fill the array?

  • Dave, from Oz (unregistered)

    This is what happens when you let functional programming weenies write your code.

  • markm (unregistered) in reply to Anonymous

    I don't speak JS either, but I think that was answered by TehShrike: "Since passing a number to the JS constructor doesn't instantiate an array of empty elements you can iterate over like most other languages".

    If I understand that correctly, you can't just declare an array and run a for loop through it. To me, that is a TWTF in the language.

    Another WTF: Why would you need an array where Array[n] == n? Why not just use n? The only time that you need an array is when you replace (some of) the original numbers with different values, and usually such arrays either need no initialization, or are initialized to zero or another null value. But apparently in JS, each cell has to be initialized, or it doesn't actually exist. That still doesn't tell me why the initialization should be 1,2,...,n.

  • David (unregistered) in reply to Dave, from Oz

    It’s gonna be so hilarious when you profile this and realize it’s faster than the loop you just wrote... please record and post on YouTube.

Leave a comment on “An Array of WHY”

Log In or post as a guest

Replying to comment #:

« Return to Article