- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
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.
Admin
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.
Admin
TRWTF is the number of commenters who can't tell whether a brace closes a function or a non-braced if statement.
Admin
Admin
<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>
Admin
Admin
Wouldn't this NEVER return 5... So FRT901Q.jpg would never be shown?
var ry=Math.floor(Math.random()*myimages.length)
Admin
Never mind, I was wrong... there are 6 items in the array not 5.
Admin
Admin
A quick test on jsFiddle shows that it will.
Admin
Innumeracy is a more important (and perhaps more widespread) problem than illiteracy.
Admin
Ahhh, Javascript arrays... don't even get me started.
Admin
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:
vs
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?Admin
I have no idea, that's why I use coffeescript.
Admin
Admin
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.
Admin
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?
Admin
(The other jokes visible in HTML comments were more sensible.)
Admin
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
Admin
To be fair, posting stupid things is the purpose of the site.
Admin
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.)
Admin
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
is valid syntax that returns undefined, but is a syntax error.Admin
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
Admin
Fuck you
Admin
Definitely indicative of someone doing XML in T-SQL!
Admin
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.
Admin
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.
Admin
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.
Admin
How long do you think the array is?
Admin
Click on "image" in "do something clever, like display a random image" and watch the random magic.
Admin
Admin
Exactly!
Admin
Admin
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.
Admin
But... what will this alert?
var arr = []; arr[-5] = "test"; alert(arr.length);
Admin
That's increased randomness for you right there.
Admin
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
Admin
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
Admin
Edited for nothing.
Admin
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.
Admin
heh.
Admin
Proof of concept:
http://jsfiddle.net/Fcafk/
Admin
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.
Admin
var arr = []; arr[1] = "test" for (var i in arr) { alert(typeof i); }
Admin
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...
Admin
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
Admin
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.
Admin
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...
Admin
Admin
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).