Think you know JavaScript? Really? Are you sure? Try the following quick quiz. Guess what each expression evaluates to. (Answers given at the end.)
1. ++Math.PI
2. (0.1 + 0.2) + 0.3 == 0.1 + (0.2 + 0.3)
3. typeof NaN
4. typeof typeof undefined
5. a = {null:null}; typeof a.null;
6. a = "5"; b = "2"; c = a * b;
7. a = "5"; b = 2; c = a+++b;
8. isNaN(1/null)
9. (16).toString(16)
10. 016 * 2
11. ~null
12. "ab c".match(/\b\w\b/)
Answers:
1. 4.141592653589793
2. false
3. "number"
4. "string"
5. "object"
6. 10
7. 7
8. false
9. "10"
10. 28
11. -1
12. [ "c" ]
For people who work with JavaScript more than occasionally, score as follows:
(correct answers: score)
12: MASTER OF THE KNOWN UNIVERSE
10 - 11: SAVANT
8 - 9: EXPERT
5 - 7: KNOWLEDGEABLE
< 5: RUSTY
NOTES
The
answer to No. 2 is the same for JavaScript as for Java (or any other
language that uses IEEE 754 floating point numbers), and it's one reason
why you shouldn't use floating point arithmetic in any serious
application involving monetary values. There's an interesting
overview here.
No.
6: In an arithmetic expression involving multiplication, division,
and/or subtraction, if the expression contains one or more strings, the
interpreter will try to cast the strings to numbers first. If the
arithmetic expression involves addition, however, all terms will be cast to strings.
No.
7: What you've got here is "a, post-incremented, plus b,"
not "a plus pre-incremented b."
No. 9: toString( )
takes a numeric argument (optionally, of course). An argument of "16"
means base-16, hence the returned string is a hex representation of 16,
which is "10." If you write .toString(2), you get a binary representation of the number, etc.
No. 10: 016
is octal notation for 14 decimal. Interestingly, though, the
interpreter will treat "016" (in string form) as base-ten if you
multiply it by one.
Don't feel bad if you didn't do well on this
quiz, because almost every question was a trick question (obviously),
and let's face it, trick questions suck ass. OTOH, if you did
well on a test that sucks, you should take full credit. It means you're no fool.
Hello, sir i would like to ask that what is the scope of java training, what all topics should be covered and it is kinda bothering me … and has anyone studies from this course http://www.wiziq.com/course/1779-core-and-advance-java-concepts of core and advance java online ?? or tell me any other guidance...
ReplyDeletewould really appreciate help… and Also i would like to thank for all the information you are providing on java concepts.
And what's the catch with the last one? It's just a simple regexp as for me.
ReplyDeleteThere must be some different score table for readers of your blog — got 9 and I'm not an expert in JavaScript at all. :)
Bullshit. I work as a JS programmer and didn't get half right.
Deletesame here, don't work with JS and I got 7, 8, 11, and 12 wrong (what's \b?)
Delete1) In order to catch the "ab", one needs the \w to repeat, e.g., \w+. Otherwise, it looks for boundary, one letter, boundary.
Delete2) In order to catch both "ab" and "c", one needs /../g flag.
Phill,
DeleteYeah, I know that. I'm saying it's just simple regexp rules as in any other language with regexps in it, not some kind of weird js magic as in expressions above.
Second Anonymous,
\b means word boundary (any symbol \W or start/end of line).
First Anonymous,
I think, it means you just haven't met this kind of stuff in your work. Yet. :)
Also, for me expert that's not a person who knows all tricks of language, but have a lot of experience in using it. And I don't.
Just my two cents: you should put a lot of whitespace between the questions and answers not to spoil it to someone accidentally.
ReplyDeletesome neat facts to be converted to flash cards ;)
ReplyDeleteThe questions number 7 and 8 were the most tricky one for me. *Good* knowledge of at least one popular programming language is enough to answer questions 1, 2, 9, 10 I guess.
ReplyDeleteMost of these have absolutely no applicable use.
ReplyDeletec = a+++b;
ReplyDeletethat is a good interview question... anyone who doesn't answer "why would anyone write stuff like that???" should never be hired.
I'm fairly new at JS, and I haven't run into any of those things yet. I've mostly programmed server side apps. Working with JS has been fun.
ReplyDeleteI've got déjà vu: http://www.planetpdf.com/developer/article.asp?ContentID=6337
ReplyDeleteOn Sunday nights Erin Jaimes hosts a blues jam where anyone from Alan
ReplyDeleteHaynes to Gary Clark, Jr. At the end of each round read out the cumulative
scores. Ask your local club to run this for you.
Also visit my blog post: pub quiz and answers