- The pieces of code are executed as statements.
- Enter your answer as if you were typing the code literally. So, if the answer is the string “foo”, then type “foo” (including the quotes — double or single), and if it’s an array like
[1,2,3]
then type it out just like that. - If you think an error is thrown then enter ERROR in the appropriate textbox.
- When there are multiple statements, type what the last statement returns.
Please access this post directly in order to take the quiz (note: JavaScript must be enabled).
Like I said, it doesn’t test your everyday knowledge of JS, but your comprehension of the specification. Feel free to post results and discuss the quiz! I’ll eventually post a comment with all the answers (plus explanations), assuming nobody beats me to it.Thanks for reading! Please share your thoughts with me on Twitter. Have a great day!
To see my results, you must calculate what the following returns:
Doh!
You got 13/25 correct.
Incorrect: #6, #7, #8, #12, #13, #14, #17, #18, #19, #21, #22, #25
Will wait for the answers 😛
You got 7/25 correct. Incorrect: #1, #2, #3, #5, #6, #7, #8, #9, #12, #13, #16, #17, #18, #19, #20, #21, #24, #25
I just wanna say, I hate you.
Here is a little bit of explanation on each question. Do not read it if you want to enjoy the quiz 😉
1. Answer: 3. 1 to bool is true, so it goes to 3 and finally returns it as there is nothing more on the right
2. Answer: “foo”. Again, 1 converted to bool is true, then we check “foo” which is also true so the remaining alternative is not checked
3. Answer: 1. 1 to bool is true and returned, because there is no need to check further conditions (what the logic clearly says ;-))
4. Answer: 3. (x, x1, x2, …, xn) always returns xn
5. Answer: 0. Looks like the array internal “shift” function automatically adds the length property to the object.
6. Answer: “0” or [0]. As {foo:1} is a block (what James described here https://j11y.io/javascript/labelled-blocks-useful/) you can completely ignore it and focus on [0]. E.g. [0].toString() gives “0”. James actually has taken “[0]” as a correct answer probably suggested by the returned value in Firebug. But IMHO it should be “0”.
7. Answer: true. Again, we meet a comma here, so let’s focus on the last item between second pair of brackets – false evaluated to the number gives 0, so the returned value is true -> [true, false][0]
8. Answer: 6. ’52’.split(”) gives an array of two string elements [“5”, “2”]. So [“5”, “2”][0] gives us “5”. ++”5″ converts “5” to the number and preincrements it so the final result is 6.
9. Answer: 5. We can completely ignore a: b: c: d: e: f: g: part because it says us we just labelled the statement on the right and nothing more. Then we evaluate a statement: 1, 2, 3, 4, 5, and as you probably now already, x1, x2, xn results with the last element.
10. Answer: Error. {a: 1, b: 2} – syntax error
11. Answer. “b45”. It does not matter where string is – left or right – this kind of concatenation always returns with a new string built by a string and a number
12. Answer: 2. http://bclary.com/2004/11/07/#a-12.1 – The block gets evaluated
13. Answer: undefined. Empty function execution returns an undefined value by default.
14. Answer: 2. The most tricky part here is 0..toString.length. Why two dots? “0..” means actually “0.0.”. So it is like (0).toString.length. Again, why? JavaScript supports only the number type, so a number could be a float and we cannot write 0.toString – JS is not sure whether to take .toString as a method or a decimal value. Why is “toString.length” equal to 1? We have to know, that .length property used with a function returns a number of declared arguments we expect. E.g.:
var fn = function(a,b){};
fn.length; // 2 as we declared “a” and “b” arguments
15. Answer: true. {} gets evaluated to the string -> “[object Object]” and a string concatenation happens then. The rest is just a logic.
16. Answer: false. We use a strict comparison here, so typeof this === “object” and typeof 123 === “number” are definitely not the same.
17. Answer: “,”. Array(2) produces a new array with two elements set on undefined. So if we cast the array to string the result is a pure comma.
18. Answer: undefined. Labelled statement again.
19. Answer: 123. The block is evaluated.
20. Answer: undefined. Introducing a new scope JS scans for every “var” occurrence, reseting each variable value to undefined. It does not matter a variable was defined before.
21. Answer: false. http://perfectionkills.com/understanding-delete/
22. Answer: 1. this.source gives you the regexp pattern content (ignoring slashes of course!). Thus, there is a simple subtraction /3/ – /2/, which evaluates to 3 – 2 and results with 1. https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Regular_Expressions
23. Answer: ERROR. “break” has to be put inside a loop or switch statement.
24. Answer: false. (new function(){ return String(‘foo’); }).toString() != ‘foo’
25. Answer: “f,o,o”. Just consider e.g. [1, 2] + [3, 4] and see how arrays are casted to string.
Cool.
You got 16/25 correct. Incorrect: #3, #6, #10, #12, #13, #16, #19, #21, #24
Nice 🙂 I’ll check my errors with the specs. I explicitly didn’t use them, although some cases are very obscure… RegExp.toSource? :p
You may want to clarify that these are executed as a script and such as statements. The leading comment “The pieces of code are expressions, statements and sometimes multiple statements” is missleading because in Javascript expressions and statements can be the same sometimes. The contexts did not make it clear for me that you were trying to get the reader in a statement vs expression trap. Either that or some of my answers are certainly correct after all 😉
Anyways, fwiw a rundown of my errors…
#3: oops, knew that but applied different logic for some reason :p
#6: interesting. When I alert it, it returns undefined, as I expected (property 0 of an object literal…). When eval-ed, it returns 0. Statement vs Expression?
#10: I was assuming it was an expression and the number 2 would have been a valid answer. I’m not sure whether stating syntax error is quite fair in this context (but true when ran as a statement, sure).
#12: would not have guessed the evaluation…
#13: missed the call, my bad 🙂
#16: meh, wrong guess. Was going for a cast :p
#19: expected object. I guess that was wrong…
#21: guessed undefined, my wrong.
#24: somehow, I missed the explicit String call. Ohwell 🙂
[/excuses] 😉
I guessed #5 btw. I don’t know the internal working of that function by heart. I also guessed that ++ was applied after the dot in #8. #22 was a sheer guess because I didn’t know what toSource would have done.
Addition to Damien:
#14: only Number.prototype.toString.length is 1. Other toString lengths are 0. Number takes a radix parameter for its toString 😉 #
18: variable hoisting causes “vars” to be defined before using it and prevents an error to be thrown.
#25: Array.prototype.toString calls Array.prototype.join. If that method gets no parameters, the komma is used as glue.
Thanks! 😀
You got 15/25 correct. Incorrect: #6, #8, #12, #14, #16, #17, #18, #21, #23, #25
😛
You got 7/25 correct. Incorrect: #1, #2, #3, #4, #6, #7, #10, #11, #13, #14, #16, #17, #20, #21, #22, #23, #24, #25
My result sucks =X
You got 15/25 correct. Incorrect: #2, #6, #10, #11, #14, #17, #20, #22, #24, #25
I enjoy very much your blog … But this morning it was to hard and to much interesting to answer your javascript Quizz. Please, if you can give more details for the answers ?
You got 8/25 correct.
Thanks for all
Thanks for having a go, everyone! 🙂
Damian (and qFox) seems to have covered all of the answers! Thanks.
Why?
[0]
returns[0]
… why do we need totoString()
it?James: try alert([0]). I was just confused by the context of running this code.
You got 19/25 correct. Incorrect: #10, #11, #13, #17, #18, #20.
You got 20/25 correct. Incorrect: #3, #6, #10, #12, #23
#3: I can’t freakin’ believe I got this one wrong! Totally my fault.
#6: This one is debatable IMHO. I can’t wait to read the rationale behind it.
#10: This one is a valid expression, but not a valid statement; I answered with the result of
{a: 1, b: 2}[["b"]]
as an expression (2) so either the question was not exceedingly clear, or… oh well, after failing #3 I’m not sure of anything any longer.#12: This one is a valid statement, resulting in
2
, but also a valid Object literal… oh wait, there’s no such thing as an Object literal in the standard (pluseval
‘ing #12 yields2
as well) so… my fault, again.#23: I wrote “SybtaxError” as an answer. LOL! That makes #23 my 21st correct answer, typos apart, I suppose.
Good quiz 😉 Hard enough.
You got 24/25 correct. Incorrect: #25
Why did I answer “foo” instead of “f,o,o”? Hm…P.S.: first I had
You got 22/25 correct. Incorrect: #10, #23, #25
because didn’t read disclamer carefully and wrote SyntaxError instead of ERROR. So, I think I answer #10, #23 correctly.
Dmitry.
Wow @ This is really awesome @
I’m not quite sure whether this is more or less useful than Kangax or Dmitry Baranovskiy’s quizzes, haha, but it was fun either way.
15/25 because I didn’t read the directions
17/25 after I did
25/25 after I used the WebKit console, 😛