• Herr Otto Flick (unregistered) in reply to tegh
    tegh:
    The real WTFs are in the comments. … Also the last image will in fact be displayed 1/6 of the time.

    You're like the 5th person to mention that about the last image. So the fuck what? There is nothing special about the last image, every other image apart from the first image will be displayed 1/6th of the time.

    Also, the second from last image will in fact be displayed 1/6 of the time.

  • Sizik (unregistered) in reply to KeroHazel
    KeroHazel:
    ratchet freak:
    heh reminds me of a riddle/js WTF:

    what does the followin equate to?

    (function(){return 1})()

    answer: undefined

    Would someone like to explain this one for me?

    Since the return is followed by a newline, it is assumed to be the end of the statement. Thus, it acts as if a semicolon was there and returns nothing.

  • hank (unregistered)

    TRWTF is the number of commenters who can't tell whether a brace closes a function or a non-braced if statement.

  • refoveo (unregistered) in reply to hank
    hank:
    TRWTF is the number of commenters who can't tell whether a brace closes a function or a non-braced if statement.
    Which probably explains the abundance of Javascript wtf's out there...
  • (cs)

    <sarcasm> TRWTF is that he didn't put the "random" number generating code in a function, then put the call to that in a while loop to ensure that it isn't 0. </sarcasm>

  • (cs) in reply to snichols
    snichols:
    an easy solution to this problem would have been to .ceil() the random instead of .floor() then his outcomes would have been 1-5 instead of 0-4.
    I suspect doing that would have led to:
    if (ry==0)
        ry=1
    else if (ry==6)
        ry=5
  • Brad Ford (unregistered)

    Wouldn't this NEVER return 5... So FRT901Q.jpg would never be shown?

    var ry=Math.floor(Math.random()*myimages.length)

  • Brad Ford (unregistered) in reply to Brad Ford

    Never mind, I was wrong... there are 6 items in the array not 5.

  • (cs) in reply to faoileag
    faoileag:
    The document.write() however definitely is a wtf: http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice

    I mean, a random image usually is expected to appear at a specific place inside the webpage, not wherever document.write feels like placing it.

    Fail. Document.write() is entirely deterministic-- it inserts text at the precise point in the document flow where the function is called. There is nothing technically wrong with using it in this way.

  • (cs) in reply to Brad Ford
    Brad Ford:
    Wouldn't this NEVER return 5... So FRT901Q.jpg would never be shown?

    var ry=Math.floor(Math.random()*myimages.length)

    A quick test on jsFiddle shows that it will.

  • FormalWare (unregistered)

    Innumeracy is a more important (and perhaps more widespread) problem than illiteracy.

  • anonymous (unregistered)

    Ahhh, Javascript arrays... don't even get me started.

  • (cs) in reply to Sizik
    Sizik:
    KeroHazel:
    ratchet freak:
    heh reminds me of a riddle/js WTF:

    what does the followin equate to?

    (function(){return 1})()

    answer: undefined

    Would someone like to explain this one for me?

    Since the return is followed by a newline, it is assumed to be the end of the statement. Thus, it acts as if a semicolon was there and returns nothing.

    And for completeness' sake: JavaScript allows expressions to be used as statements, meaning the free-floating '1' is valid as well.

    There's also a far more insidious example of white-space screwing over JavaScript code, which is related to brace placement:

    (function(){
      return {
        foo : "bar"
      }
    })();
    

    vs

    (function(){
      return
      {
        foo : "bar"
      }
    })();

    The first will correctly return the object, whereas the latter will again return undefined. For bonus points, and to illustrate how deep the rabbit hole goes: what does the following code return and why does it do what it does?

    (function(){
      return
      {
        "foo" : "bar"
      }
    })();
  • Meep (unregistered) in reply to hank
    hank:
    TRWTF is the number of commenters who can't tell whether a brace closes a function or a non-braced if statement.

    I have no idea, that's why I use coffeescript.

  • hank (unregistered) in reply to Meep
    Meep:
    hank:
    TRWTF is the number of commenters who can't tell whether a brace closes a function or a non-braced if statement.
    I have no idea, that's why I use coffeescript.
    Because Javascript is the only language that uses braces in its syntax.
  • EvilCodeMonkey (unregistered) in reply to faoileag
    faoileag:
    So, the author doesn't know (or doesn't care) that Javascript arrays are zero-based, practices bad indentation and seems to have difficulties with generating a random number from the range 1..5, leading to image 1 being shown more often than the others.

    If this code indeed hails from the late 90s, then this has to be considered normal or at least as soemthing that does not merit a wtf. In that epoch, Javascript stuff like that usually was hacked together by webmasters not by programmers.

    The document.write() however definitely is a wtf: http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice

    I mean, a random image usually is expected to appear at a specific place inside the webpage, not wherever document.write feels like placing it.

    If the image is in the same place every time, it's not a truly random image. The writer was merely ensuring all of the meanings were covered.

    Though he missed out on the random noise image in a random size also. Noob.

  • Norman Diamond (unregistered) in reply to ObiWayneKenobi
    ObiWayneKenobi:
    The 1-based array is indicative of a Visual Basic coder.
    I thought the 1-based array was indicative of a Fortran coder. The saying used to be that a real programmer could write Fortran in any language.

    Someone else said Pascal. I wonder what dialect of Pascal it was, where every integer subrange a..b had to have value 1 for a?

  • Norman Diamond (unregistered)
    Remy:
    <!-- <b>The End is Nigh! The Daily WTF's OMGWTF2 Contest Ends Today!! </b> - Prizes! Fame! Trophies! Do your worst: <a href="http://omg2.thedailywtf.com/">http://omg2.thedailywtf.com/</a> -->
    WTF?

    (The other jokes visible in HTML comments were more sensible.)

  • alex (unregistered)

    Hello I am alewx wachowski and I am a gigjantec faggot OK I run thedailywtf I'm so fuckin gay I post stupid shit and eat dicks I'm retaerded

  • Standards (unregistered) in reply to alex
    alex:
    Hello I am alewx wachowski and I am a gigjantec faggot OK I run thedailywtf I'm so fuckin gay I post stupid shit and eat dicks I'm retaerded

    To be fair, posting stupid things is the purpose of the site.

  • (cs) in reply to ObiWayneKenobi
    ObiWayneKenobi:
    The 1-based array is indicative of a Visual Basic coder.
    No, not really. Arrays in VB are 0-based by default unless you explicitly change the lower bound.

    What you might be thinking of is the fact that certain collections, and even then only collections that were introduced in later versions of VB, like the SubItems collection of the ListItem, were 1-based. (And yes, this was a massively stupid and confusing decision on the part of Microsoft.)

  • Mr.'; Drop Database -- (unregistered) in reply to Ragnax
    Ragnax:
    For bonus points, and to illustrate how deep the rabbit hole goes: what does the following code return and why does it do what it does?
    (function(){
      return
      {
        "foo" : "bar"
      }
    })();
    That surprised me! It's a syntax error because {"foo" : "bar"} is interpreted as a block of code (like if it said if(true){"foo":"bar"}) instead of an object literal.

    The {foo: "bar"} example is also treated as a block of code, but it's valid because it's just an expression statement with a label. If we take this even further then

    (function(){
    return
    {
    foo: "bar"
    baz: "quz"
    }
    })();
    is valid syntax that returns undefined, but
    (function(){
    return
    {
    foo: "bar",
    baz: "quz"
    }
    })();
    is a syntax error.

  • Leo (unregistered)

    The Real WTF is the idea that there could be someone here not old enough to remember the late 90s. If you don't remember that, get off your mommy and daddy's modem.

    Sincerely, Bert Glanstron

  • alex (unregistered) in reply to Leo
    Leo:
    The Real WTF is the idea that there could be someone here not old enough to remember the late 90s. If you don't remember that, get off your mommy and daddy's modem.

    Sincerely, Bert Glanstron

    Fuck you

  • (cs) in reply to Norman Diamond
    Norman Diamond:
    ObiWayneKenobi:
    The 1-based array is indicative of a Visual Basic coder.
    I thought the 1-based array was indicative of a Fortran coder. The saying used to be that a real programmer could write Fortran in any language.

    Someone else said Pascal. I wonder what dialect of Pascal it was, where every integer subrange a..b had to have value 1 for a?

    Definitely indicative of someone doing XML in T-SQL!

  • (cs)

    This just shows how web "programmers" are not really programmers. We interviewed a junior programmer for a position that will be working with C programming on embedded devices and he had been doing PHP for 4 years and said he hadn't used C for 8 years. We gave him our sample FizzBuzz question (not really doing a FizzBuzz, it does some network tasks). His solution did some interesting machinations to avoid using pointers and arrays like they're supposed to be used. He did solve the problem though, but it was really hard to read.

  • Christian (unregistered)

    Also, semi colons are (of course) nothing that brings value...

    Notice the apparent lack of testing - since the myimages[5] is never shown - again due to the direct convertion from VBScript.

  • I forget (unregistered)

    The real WTF is zero based arrays. Nobody in the real world starts counting from zero. High-level computer languages are supposed to exist to make implementing business logic easer, but no... we have to make everything arcane.

  • Taemyr (unregistered) in reply to Christian
    Christian:
    Notice the apparent lack of testing - since the myimages[5] is never shown - again due to the direct convertion from VBScript.

    How long do you think the array is?

  • su (unregistered)

    Click on "image" in "do something clever, like display a random image" and watch the random magic.

  • jugis (unregistered) in reply to Herr Otto Flick
    Herr Otto Flick:
    You're like the 5th person to mention that about the last image. So the fuck what?
    They're correcting someone who claimed that the last image wouldn't be shown, and since people are still claiming that even after it's been debunked, I think a little point-hammering is justified.
  • Sir Galahad the Pure (unregistered) in reply to tegh
    tegh:
    The real WTFs are in the comments. It's a zero based array; element 0 isn't filled but it still counts, so the array size is 6.

    Exactly!

    var arr = [];
    arr[7] = "test";
    alert(arr.length); // will alert 8
    
  • anonymous (unregistered) in reply to Christian
    Christian:
    Also, semi colons are (of course) nothing that brings value...

    Notice the apparent lack of testing - since the myimages[5] is never shown - again due to the direct convertion from VBScript.

    Wrong! Refer to any of the countless other correct posts for why you are wrong.

  • superfrou (unregistered)

    I also want to point out that since Math.random is 0 inclusive, 1 exclusive, and they are flooring the result to get their index, shoulnd't the 5th image never be shown (floor of .99*5=4)? That would make the frequency of the first image 2/5.

  • anonymous (unregistered) in reply to Sir Galahad the Pure

    But... what will this alert?

    var arr = []; arr[-5] = "test"; alert(arr.length);

  • Scourge of Programmers! (unregistered) in reply to faoileag
    faoileag:
    So, the author doesn't know (or doesn't care) that Javascript arrays are zero-based, practices bad indentation and seems to have difficulties with generating a random number from the range 1..5, leading to image 1 being shown more often than the others.

    If this code indeed hails from the late 90s, then this has to be considered normal or at least as soemthing that does not merit a wtf. In that epoch, Javascript stuff like that usually was hacked together by webmasters not by programmers.

    The document.write() however definitely is a wtf: http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice

    I mean, a random image usually is expected to appear at a specific place inside the webpage, not wherever document.write feels like placing it.

    That's increased randomness for you right there.

  • asdf (unregistered)

    perhaps there is another explanation The requirements could be we want this image to be displayed twice as often as any other image. Now instead of doing the obvious thing as adding the same image to the array twice he decided to do his hack instead Or most likely a LUA programmer who had to write some javascript

  • Rollyn01 (unregistered) in reply to anonymous
    anonymous:
    But... what will this alert?

    var arr = []; arr[-5] = "test"; alert(arr.length);

    Really? I mean really? Depending on the language/version, Out of Bounds or Out of Scope, Illegal Arguement, Undefined Variable, Null Expcetion (forgot the exactly error) and possibly last but not least, Syntax Error.

    Yeah, you made me sweat on that one. ;-D

  • Rollyn01 (unregistered) in reply to Rollyn01
    Rollyn01:
    anonymous:
    But... what will this alert?

    var arr = []; arr[-5] = "test"; alert(arr.length);

    Really? I mean really? Depending on the language/version, Out of Bounds or Out of Scope, Illegal Arguement, Undefined Variable, Null Exception (forgot the exactly error) and possibly last but not least, Syntax Error.

    Yeah, you made me sweat on that one. ;-D

    Edited for nothing.

  • (cs) in reply to Gunslnger
    Gunslnger:
    This just shows how web "programmers" are not really programmers. We interviewed a junior programmer for a position that will be working with C programming on embedded devices and he had been doing PHP for 4 years and said he hadn't used C for 8 years. We gave him our sample FizzBuzz question (not really doing a FizzBuzz, it does some network tasks). His solution did some interesting machinations to avoid using pointers and arrays like they're supposed to be used. He did solve the problem though, but it was really hard to read.

    Absolutely not true. Each field has its own idiots. There are plenty of "mainframe" developers that I've interviewed for web development positions that fail FizzBuzz-esque test questions.

  • (cs) in reply to superfrou
    superfrou:
    I also want to point out that since Math.random is 0 inclusive, 1 exclusive, and they are flooring the result to get their index, shoulnd't the 5th image never be shown (floor of .99*5=4)? That would make the frequency of the first image 2/5.

    heh.

    chubertdev:
    Brad Ford:
    Wouldn't this NEVER return 5... So FRT901Q.jpg would never be shown?

    var ry=Math.floor(Math.random()*myimages.length)

    A quick test on jsFiddle shows that it will.

  • (cs)

    Proof of concept:

    http://jsfiddle.net/Fcafk/

  • (cs) in reply to Rollyn01
    Rollyn01:
    anonymous:
    But... what will this alert?

    var arr = []; arr[-5] = "test"; alert(arr.length);

    Really? I mean really? Depending on the language/version, Out of Bounds or Out of Scope, Illegal Arguement, Undefined Variable, Null Expcetion (forgot the exactly error) and possibly last but not least, Syntax Error.

    Yeah, you made me sweat on that one. ;-D

    It alerts 0 in JavaScript.

    Even though negative numerical indices are not supported by the array-indexing operations in JavaScript's arrays, they are still perfectly valid property names (once type-converted to strings), meaning the array is treated as a regular object and gets a property named "-5" added without affecting the array data.

  • anonymous (unregistered) in reply to Ragnax
    Ragnax:
    Rollyn01:
    anonymous:
    But... what will this alert?

    var arr = []; arr[-5] = "test"; alert(arr.length);

    Really? I mean really? Depending on the language/version, Out of Bounds or Out of Scope, Illegal Arguement, Undefined Variable, Null Expcetion (forgot the exactly error) and possibly last but not least, Syntax Error.

    Yeah, you made me sweat on that one. ;-D

    It alerts 0 in JavaScript.

    Even though negative numerical indices are not supported by the array-indexing operations in JavaScript's arrays, they are still perfectly valid property names (once type-converted to strings), meaning the array is treated as a regular object and gets a property named "-5" added without affecting the array data.

    Ok... -5 gets type-converted to string because it's "different". What do you think this will result in, then?

    var arr = []; arr[1] = "test" for (var i in arr) { alert(typeof i); }

  • Marcus (unregistered)

    Submitter here.

    Just want to put this out there: this site first went live waaaaay back in December of 2012.

    Captcha: decet. (adj), the level of quality we can usually expect from our overseas front-end devs...

  • harryhashtable (unregistered) in reply to faoileag

    The correct solution is to use something like Fisher-Yates. Rob Weir has a excellent blog on this problem.

    http://www.robweir.com/blog/2010/02/microsoft-random-browser-ballot.html

  • (cs) in reply to anonymous
    anonymous:
    Ragnax:
    Rollyn01:
    anonymous:
    But... what will this alert?

    var arr = []; arr[-5] = "test"; alert(arr.length);

    Really? I mean really? Depending on the language/version, Out of Bounds or Out of Scope, Illegal Arguement, Undefined Variable, Null Expcetion (forgot the exactly error) and possibly last but not least, Syntax Error.

    Yeah, you made me sweat on that one. ;-D

    It alerts 0 in JavaScript.

    Even though negative numerical indices are not supported by the array-indexing operations in JavaScript's arrays, they are still perfectly valid property names (once type-converted to strings), meaning the array is treated as a regular object and gets a property named "-5" added without affecting the array data.

    Ok... -5 gets type-converted to string because it's "different". What do you think this will result in, then?

    var arr = []; arr[1] = "test" for (var i in arr) { alert(typeof i); }

    Trick question. That depends on whether or not someone has been messing with Array.prototype beforehand. Assuming no-one has, it will alert "string" once.

    A for...in loop will iterate over all properties that are enumerable. For an Array, by default this consists of all indices for which a value has been set.

    The only index that actually exists on the array is index 1. (In this example arr.length is 2, because the highest populated index is 1, not because the array has magically hydrated all preceding indices with null / undefined values. That's simply not how arrays work.) It is (provided the Array prototype was left alone) also the only enumerable property, meaning it is the only property to be iterated over in for...in loops.

    A for...in loop iterates over property names and not numerical indices, meaning the array indices are accessed as strings. The loop variable i is then of type "string". The value "string" will be alerted once, for index 1 only.

  • anonymous (unregistered) in reply to Ragnax
    Ragnax:
    anonymous:
    Ok... -5 gets type-converted to string because it's "different". What do you think this will result in, then?

    var arr = []; arr[1] = "test" for (var i in arr) { alert(typeof i); }

    Trick question. That depends on whether or not someone has been messing with Array.prototype beforehand. Assuming no-one has, it will alert "string" once.

    That was the assumption, and that's correct, it alerts "string". So TRWTF is that in Javascript, array indices are stored as strings?

    Unless for...in is explicitly casting the value to a string. There's no good reason for it to do that, since Javascript is loosely typed, but you never know...

  • (cs) in reply to Standards
    Standards:
    alex:
    Hello I am alewx wachowski and I am a gigjantec faggot OK I run thedailywtf I'm so fuckin gay I post stupid shit and eat dicks I'm retaerded

    To be fair, posting stupid things is the purpose of the site.

    +1

  • acsi (unregistered)

    Simple: the author is a VB developer (where arrays are 1-based), or the author didn't like substracting 1 from "myimages.length" before multiplying it with "Math.random()" (or both).

Leave a comment on “Indexed Image”

Log In or post as a guest

Replying to comment #:

« Return to Article