Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Oh shoot. I just posted an answer with the same basic concept. I had been scanning the page for "JavaScript" or anything showing HTML tags. I suppose I might leave my answer up, just because it handles the corner-case where the location contains a "#". Anyway, +1.
@Derek朕會功夫 setInterval is not a statement, though; it's only a function. It's used inside an expression statement, and if we can't use expression statements, then I just don't even know anymore.
I had thought about doing exactly this, since it's a genuine common error for new programmers, and going to random.org is a perfect excuse. Might I suggest replacing +xhr.responseText with xhr.responseText|0 so that it looks even more like a reliable integer coercion?
@Ypnypn One of the rules currently says, "you may not use scientific notation to enter a number". The rules appear to barely leave open the possibility for our code to use scientific notation inside a string literal. Is that so? I suppose eval("9e9") still isn't quite as large as 9999999999.
@Matt I mean that |0, if you know what it does, looks like it's rounding down, and it is rounding down, so it's not a deception. (And if someone has no idea what |0 does, then there's no use for deceptive code; you can just tell them whatever you want them to believe.) Instead, the unexpected behavior in your answer is based on the fact that Math.random(0,4) is functionally identical to Math.random(), because Math.random doesn't use parameters.
@edc65 You know, I'd wanted to use a string, and I had completely blanked on the fact that I could coerce the result back to a number. A bizarre lapse. Yet I still wouldn't have come up with -(x!=6). Thank you.
At first, I nearly posted function(x)[0,-1,5,-1,-1,2,9,-1,8,6][x], also thanks to Firefox. I wasn't going to win anyway, so I decided I'd just stick with the highest-compatibility answer. If I start switching languages for brevity, then I'll eventually start defining my own language for every challenge I do. But I'll go ahead and mention the ECMAScript 6 version anyway, since you suggested it