• Etherow (unregistered)

    Well the frist image is bound to be the best.

  • (cs) in reply to Etherow
    Etherow:
    Well the frist image is bound to be the best.

    and the last image must be the worst

  • (cs)

    I must be missing something. How does 2/5 come to 1/3? Also, why no mention of the fact that it never displays the last image?

  • Mike (unregistered)

    So a small code snipped from way back has a couple of bugs that end-users probably never noticed!

    Where's the WTF?

  • Joe (unregistered) in reply to pjt33

    myimages.length returns 5 making the possible values it returns, 0, 1, 2, 3, 4, 5 (6 options). 0 is effectively 1 so the probability of it showing the first image (returning 0 or 1) is 2/6 or 1/3. This also means it would return the last image 1/6 of the time.

  • DonaldK (unregistered)

    Pretty lame really.

  • DonaldK (unregistered) in reply to Joe
    Joe:
    myimages.length returns 5 making the possible values it returns, 0, 1, 2, 3, 4, 5 (6 options). 0 is effectively 1 so the probability of it showing the first image (returning 0 or 1) is 2/6 or 1/3. This also means it would return the last image 1/6 of the time.

    Dunno but I feel like I'm being trolled...

    Zero is removed, not replaced. Permutations thus include 1, 2, 3, 4, 5, which add up to a total of 5, not 6.

    But yes, it seems that

  • QJo (unregistered)

    Probably arose when the boss said: "Every pretty damn often it duzzn work! Fix it!"

    "Oh yeah," replies Roger the Bodger. It's returning a zero. Damn, haven't got time to work out how to fix it properly, oh sod it just make it 1. That gets him off my back. Who gives a damn anyway? Now back to (enter cool game of choice for the era this was written)."

  • QJo (unregistered)

    ... but the real WTF is indenting the "document.write" command to the same level as the "ry = 1" command so as to fool the incautious developer into thinking it is part of the if clause. Oh veeeeeeery cunning.

  • Jaggerbush (unregistered) in reply to QJo

    Would have been better if ry=1 and document.write were in { }. Zen it would be totally random generator of first image.

  • ANON (unregistered) in reply to Joe
    Joe:
    myimages.length returns 5 making the possible values it returns, 0, 1, 2, 3, 4, 5 (6 options). 0 is effectively 1 so the probability of it showing the first image (returning 0 or 1) is 2/6 or 1/3. This also means it would return the last image 1/6 of the time.

    myimages.length actually returns 6, but the rest of your argumentation is right.

  • Honnza (unregistered) in reply to QJo

    well, it is inside the if clause. In effect, the first image is displayed 1/6 of the time, and nothing is shown in any other case...

  • Antonio Segni (unregistered)

    Arrays in Javascript are 0-based, so there is an array element with index = 0, although it has no value. So the array length is 6, and the random function will return a value between 0 and 5: all elements can be retrieved, but the element with index 1 is twice as likely to be chosen.

  • Balu (unregistered) in reply to Honnza
    Honnza:
    well, it _is_ inside the if clause. In effect, the first image is displayed 1/6 of the time, and nothing is shown in any other case...

    Do you see an opening bracket after the if? I don't...

  • (cs) in reply to ANON
    ANON:
    myimages.length actually returns 6
    That's what I was missing. Thanks.
  • faoileag (unregistered)
    the article said:
    when [displaying a random image] was the coolest trick you could build into a web page
    Are you insinuating that this is no longer the case with your use of the past tense here???
  • RandomGuy (unregistered)

    It's not valid Javascript anyways without semicolons ending the statements. But I guess most browsers are quite tolerant w.r.t that.

  • faoileag (unregistered)

    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.

  • (cs) in reply to RandomGuy
    RandomGuy:
    It's not valid Javascript anyways without semicolons ending the statements. But I guess most browsers are quite tolerant w.r.t that.

    The ECMAScript standard allows omitting the semicolons in some cases, but they are recommended to avoid some ambiguities.

  • faoileag (unregistered) in reply to RandomGuy
    RandomGuy:
    It's not valid Javascript anyways without semicolons ending the statements. But I guess most browsers are quite tolerant w.r.t that.
    Indeed they are.

    Then you run your code through a minifier and find out that the result doesn't work anymore.

    Luckily you can find the missing semicolons easily enough with a regular expresion or two.

  • QJo (unregistered) in reply to Honnza
    Honnza:
    well, it _is_ inside the if clause. In effect, the first image is displayed 1/6 of the time, and nothing is shown in any other case...

    No it's not. Go away and study JavaScript 101 and then report back as to why it's not inside the if clause.

  • Amakudari (unregistered) in reply to Honnza
    Honnza:
    well, it _is_ inside the if clause. In effect, the first image is displayed 1/6 of the time, and nothing is shown in any other case...

    It's a bracketless if statement. Just paste this in your browser's dev console or whatever:

    if (1 == 0)
      alert("nope")
      alert("yup")
    

    (Misleading indent intentional)

  • Amakudari (unregistered) in reply to Amakudari
    Amakudari:
    (Misleading indent intentional)

    As is the == comparison and missing semicolons. Just making sure the context is appropriate for when you might see it in the wild.

  • pymo (unregistered)

    Wow, I first read that still in Python mode; which would mean:

    if (ry==0) { ry=1 document.write('[image]') } A big WTF.

    Seeing the entire picture, maybe they wanted to get the first picture the most exposure, if so the WTFness of the article diminishes greatly.

  • Krunt (unregistered)

    Ahh, another WTF scraped from the scratched-up bottom of the barrel. Yay.

  • MrBester (unregistered)

    Janeane Garofolo

    Captcha: quis. Exactly. Quis est Jeanne Garofolo?

  • EvilSnack (unregistered)

    I'm guessing what happened is that the original coder believed that the random expression would (in the case cited here) return a value from 1 to 5. The mysteriously, there was an error, and someone told him that the expression could return zero at times. So he coded to deal with the zero.

    I'd rate this about one and a half stars out of four.

  • Kanitatlan (unregistered) in reply to Joe

    If the length is indeed 5 then the random code selects a number in the range 0 to 4 and the 5th image is never shown. We end up with 40% image 1, 20% each for images 2,3 & 4 and never show 5. It may not be desperately horrible code but it is reasonably obvious that it really doesn't do what was intended.

  • (cs)

    heh reminds me of a riddle/js WTF:

    what does the followin equate to?

    (function(){return 1})()

    answer: undefined

  • It's Pat (unregistered) in reply to DonaldK
    DonaldK:
    Joe:
    myimages.length returns 5 making the possible values it returns, 0, 1, 2, 3, 4, 5 (6 options). 0 is effectively 1 so the probability of it showing the first image (returning 0 or 1) is 2/6 or 1/3. This also means it would return the last image 1/6 of the time.

    Dunno but I feel like I'm being trolled...

    Zero is removed, not replaced. Permutations thus include 1, 2, 3, 4, 5, which add up to a total of 5, not 6.

    But yes, it seems that

    You're not being trolled. The quoted if statement is as follows: if (ry==0) ry=1 document.write('[image]') }

    So, we have a randomizer that will create a number between [0..6). Anytime a 0 occurs, it is changed to a 1. So, the total possible values are [1,1,2,3,4,5].

    But the other problem I see is that an image will only be displayed when the randomizer returns a 0 value. The document.write statement is within the if clause. And, as such, it will only ever display image 1.

    Of course, if the code read as such: if (ry==0) ry=1 } document.write('[image]')

    Then an image is displayed every time and the first image as a 1/3rd chance of being displayed.

  • It's Pat (unregistered)

    Okay, I'm stupid and got caught by the misleading indentation of the document.write statement. Sorry.

    But that doesn't detract from the results of the randomizer and if statement combination returning any one of [1,1,2,3,4,5]

  • (cs)

    Honestly, looks like the kind of code an inexperienced developer might write when just learning and needing to fulfill a requirement to have random images. The 1-based array is indicative of a Visual Basic coder.

    Certainly a WTF, but one that's understandable, for whatever that's worth.

  • (cs)

    Googling the comment in the code, it looks like this was copied and pasted from one of many JavaScript sites (eg. I found it at http://www.javascriptkit.com/script/cut144.shtml). Most likely the developer didn't know what they were actually doing.

  • (cs) in reply to Kanitatlan
    Kanitatlan:
    If the length is indeed 5 then the random code selects a number in the range 0 to 4 and the 5th image is never shown. We end up with 40% image 1, 20% each for images 2,3 & 4 and never show 5. It may not be desperately horrible code but it is reasonably obvious that it really doesn't do what was intended.
    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.
  • (cs) in reply to QJo
    QJo:
    Probably arose when the boss said: "Every pretty damn often it duzzn work! Fix it!"

    "Oh yeah," replies Roger the Bodger. It's returning a zero. Damn, haven't got time to work out how to fix it properly, oh sod it just make it 1. That gets him off my back. Who gives a damn anyway? Now back to Quake III Arena."

    HUMILIATION EXCELLENT

  • QJo (unregistered) in reply to QJo
    QJo:
    ... but the real WTF is indenting the "document.write" command to the same level as the "ry = 1" command so as to fool the incautious developer into thinking it is part of the if clause. Oh veeeeeeery cunning.

    Hmm ... it seems this has caught out plenty of punters today ... it's a bit like dropping a penny in front of a marathon. The leader will of course stop dead to pick it up and everybody will pile into him causing an amusing pile of mayhem.

  • Rodrigo (unregistered) in reply to Honnza
    Honnza:
    well, it _is_ inside the if clause
    Python programmer detected
  • (cs) in reply to Kanitatlan
    Kanitatlan:
    If the length is indeed 5 then the random code selects a number in the range 0 to 4 and the 5th image is never shown. We end up with 40% image 1, 20% each for images 2,3 & 4 and never show 5. It may not be desperately horrible code but it is reasonably obvious that it really doesn't do what was intended.
    the kicker here is that the length is not 5, its 6 (remember, the indexes in the array are [0,1,2,3,4,5] totaling, 6 not 5.) this means that the random number can be anything from 0-5. but we want 1-5, so 0 is replaced with 1. obvious copy-dontunderstand-paste job.
  • faoileag (unregistered) in reply to Daniel15
    Daniel15:
    (eg. I found it at http://www.javascriptkit.com/script/cut144.shtml).
    www.javascriptkit.com/script/cut144.shtml:
    This is a robust script that loads a random image...
    An interesting definition of "robust" that poster has...
  • qbolec (unregistered)

    the real WTF are:

    • not using templating engine
    • using new Array() instead of []
    • indexing from 1
    • indenting code badly
    • not using curly braces
    • not using semicolons
    • believing that minifiers can not handle missing semicolons
    • believing that .ceil would help (forgetting that Math.random can return 0)
    • believing that .length == 5 (forgetting that missing array elements are initialized to undefined)
    • hardcoding links
    • forgetting about escaping html entities in attributes (or forcing the array to contain already escaped links which is another wtf, or believing that & can never occur in a url, which is kind of naive)
    • document writing instead of injecting node into DOM
    • writing in JS before all above obstacles become easy
    • naming a function/method with a noun
    • using border=0 instead of CSS
    • calling a variable "ry" (random ymage?)
    • manually indexing elements of the array during initialization instead of using JSON literal initialization or .push
    • defining a function in global scope
    • suggesting in the comment that array already should contain random images
  • faoileag (unregistered) in reply to Daniel15
    Daniel15:
    Googling the comment in the code, it looks like this was copied and pasted from one of many JavaScript sites
    the article said:
    Those of us old enough to remember the late 90s might recall an era when that was the coolest trick you could build into a web page
    For some it still is: http://froghat.co/post/19901960118/omg-how-did-you-make-it-so-that-your-sidebar-gif
  • foo (unregistered) in reply to qbolec
    qbolec:
    the real WTF are: * not using templating engine * using new Array() instead of [] * indexing from 1 * indenting code badly * not using curly braces * not using semicolons * believing that minifiers can not handle missing semicolons * believing that .ceil would help (forgetting that Math.random can return 0) * believing that .length == 5 (forgetting that missing array elements are initialized to undefined) * hardcoding links * forgetting about escaping html entities in attributes (or forcing the array to contain already escaped links which is another wtf, or believing that & can never occur in a url, which is kind of naive) * document writing instead of injecting node into DOM * writing in JS before all above obstacles become easy * naming a function/method with a noun * using border=0 instead of CSS * calling a variable "ry" (random ymage?) * manually indexing elements of the array during initialization instead of using JSON literal initialization or .push * defining a function in global scope * suggesting in the comment that array already should contain random images
    * not using xkcd's random generator * Visual Basic * PHP * Hanzo
  • wtf? (unregistered)

    I think qbolec wins some internets.

  • (cs)

    That's not a mistake. It's an NSA backdoor meant to look like a mistake by subverting the PRNG.

  • faoileag (unregistered) in reply to qbolec
    qbolec:
    * believing that minifiers can not handle missing semicolons
    Well, they say that "seeing is believing" and I saw and I believed (especially since adding the semicolon turned out indeed to be the remedy). Note to qbolec: it is also a WTF to think that all minifiers behave the same.
    qbolec:
    * writing in JS before all above obstacles become easy
    Yes writing for instance in C++ would be so much more comfortable! A missing ";" at the end of the file? C++ compilers have a special error message just for the fact that the last character is not a semicolon. If they can detect that special case, why not ignore it altogether?
    qbolec:
    * naming a function/method with a noun
    Indeed. random, floor and ceil should definitely be renamed and btw it's "celiling" not "ceil"!
    qbolec:
    * calling a variable "ry" (random ymage?)
    random ypsilon. It's the vertical axis, you see. After all, the array elements are set one below the other.
  • faoileag (unregistered) in reply to qbolec
    qbolec:
    the real WTF are: * manually indexing elements of the array during initialization instead of using (...) .push
    Another WTF is assuming that the push method was available at the time the script was originally written.

    push has been added as part of Javascript 1.3 (http://web.archive.org/web/20000815081640/http://developer.netscape.com/docs/manuals/communicator/jsref/js13.html) and the script might predate that (no, I did not check whether functions used in the script indicate that it can not have originated before Javascript 1.3).

  • KeroHazel (unregistered) in reply to ratchet freak
    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?

  • noland (unregistered) in reply to qbolec
    qbolec:
    the real WTF are: * not using templating engine * using new Array() instead of [] * indexing from 1 * indenting code badly * not using curly braces * not using semicolons * believing that minifiers can not handle missing semicolons * believing that .ceil would help (forgetting that Math.random can return 0) * believing that .length == 5 (forgetting that missing array elements are initialized to undefined) * hardcoding links * forgetting about escaping html entities in attributes (or forcing the array to contain already escaped links which is another wtf, or believing that & can never occur in a url, which is kind of naive) * document writing instead of injecting node into DOM * writing in JS before all above obstacles become easy * naming a function/method with a noun * using border=0 instead of CSS * calling a variable "ry" (random ymage?) * manually indexing elements of the array during initialization instead of using JSON literal initialization or .push * defining a function in global scope * suggesting in the comment that array already should contain random images

    As someone who actually lived then: The style of code indicates an author who would have been at home with AWK and maybe had some background in Pascal. (Note that it's not just simple plain code, but is enclosed in a named function, this tells of some discipline, while the missing semicolons are expressing some contempt for JS, as expressed by many "serious" IT folks of the time.)

    Now (opinions expressed here are/were not mine, but actually existed):

    • template engine: didn't exist then
    • anonymous arrays ([]): where not supported by IE3
    • indexing from 1: but this is like Pascal and AWK do it (flimsy C-style!)
    • indenting code badly: Probably refactored, but white-space is insignificant anyway (not a serious assembler code)
    • not using curly brackets: Don't start a block, if you do not have to (see Pascal)
    • not using semicolons: "JS is not a serious language!" (don't do it any honor)
    • minifiers: didn't exist then (or were quite esoteric)
    • document.write(): Other means (node insertion) didn't exist and became available with IE4 and NS6
    • manually indexing: Now we want to express our disdain for C-style syntax, meaning we have to skip the first item. So it's the only way to do it (and this style was quite common – but wasn't considered a good style). Also, this is how you would do it in Pascal, which is best practice.
    • Using CSS: only with available with IE4+, NS4 had quite unstable support (unknown by IE3, NS3, which were still around)
    • JSON: What's that? (90s)
    • named function in global scope: now this is the most "honorable" part of the script. (As there were no libraries and usually only few code in a single script, this wasn't a problem. Also: anonymous functions in IE3? Much fun!)
    • "ry": already covered in other comment. (The mental model using the "y"-axis tells of the seasoned programmer.)
  • tegh (unregistered) in reply to pjt33

    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.

    ry when initially set, has an equal probability distribution of among values 0, 1, 2, 3, 4, or 5. P(0)=(1/6) and P(1)=(1/6), so the probability of the first image being displayed is actually 1/3.

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

  • (cs) in reply to Etherow
    Etherow:
    Well the frist image is bound to be the best.

    if(post=='zreoth') post=='frist'

Leave a comment on “Indexed Image”

Log In or post as a guest

Replying to comment #:

« Return to Article