

JavaScript Quiz Set - bolshchikov
http://blog.bolshchikov.net/post/40917260776/javascript-quiz-set

======
raju
Interesting. I just started this, and came back here to post this.

The third question - "Global Variables Intro" in the "beginner" (one by
<http://madebyknight.com/javascript-scope/>) is not quite right.

    
    
      function() {
          a = 3;
          alert(a);
      }
    

is _not_ the same as

    
    
      var a;
      function() {
          a = 3;
          alert(a);
      }
    

Whenever you don't declare a variable with a _var_ , it does not become a
global variable, rather, it becomes a property of the global (or host) object.

The main difference here is that you _can_ delete a property -

    
    
      delete window.a;
    

But you can't delete 'a' if it were defined by _var_.

In the same light, the following statement (IMO) is incorrect.

    
    
      When Javascript comes across a global variable, it needs somewhere to put it. It needs somewhere that everywhere else on the page can access. So, it uses the window object. 
    

Still working through the tutorial ...

Edit - Copy pasted the wrong sentence from the quiz. Sorry

~~~
bolshchikov
Global variables are in fact properties of the global object. In web pages the
global object is window, so you can set and access global variables using the
window.variable syntax. [https://developer.mozilla.org/en-
US/docs/JavaScript/Guide/Va...](https://developer.mozilla.org/en-
US/docs/JavaScript/Guide/Values,_variables,_and_literals?redirectlocale=en-
US&redirectslug=Core_JavaScript_1.5_Guide%2FValues%2C_Variables%2C_and_Literals#Variables)

------
skylan_q
I guess some of this knowledge is helpful or necessary for people who want to
know JS inside-out. Nevertheless, none of that code looked like anything a
programmer would write if they respected readability and conveyance of code.
Maybe I'm just taking things too seriously here...

A code snippet like the following wouldn't make me ask how it works, but it
would make me want to find the person responsible for putting this snippet in
production code:

var num1 = 5, num2 = 10, result = num1+++num2;

(I'm probably thinking in this manner these days because I'm currently in a
situation where I'm maintaining someone else's code instead of building
something.)

~~~
prophetjohn
The above example is at least academically interesting since you can reason
through it with a bit of programming experience and intuition about operator
precedence.

    
    
        Does plus or pre/postfix increment have higher precedence?
        Probably increment.
        Does prefix or postfix have higher precedence?
        They're probably the same -> evaluate from left-to-right.
        Code is equivalent to num1++ + num2;
    
    

But the examples like "what is the difference between substring() and
substr()", "What is the lowest cross-browser increment that
[setInterval()/setTimeout()] can accurately use" is just trivia and is boring.

~~~
jonsen
You also need to know how tokens are produced by lexical analysis. It could
have produced:

    
    
      'num1' '+' '++' 'num2'
    

In fact it produces

    
    
      'num1' '++' '+' 'num2'
    

so it's not about _evaluate from left-to-right_.

~~~
prophetjohn
Is there a case in JavaScript where operators with the same precedence are not
evaluated left-to-right?

~~~
jonsen
I don't know. But I happen to know that the javascript tokenizer matches the
longest possible substring. Therefore it takes '++' from '+++..', not '+'.
It's not about precedence.

EDIT: Yes, the assignment operators, =, += etc., evaluates right to left.

------
strager
[SPOILERS]

I got tripped up by question #5 of one of the expert quizzes [1] due to a
difference between ActionScript 3 and JavaScript [2].

In AS3, `(a).b` is equivalent to JS's `(null, a).b`. That is, the `this`
binding is lost in AS3 (but not JS). I thus thought the anser was "20, 10, 10,
10", and not "20, 20, 10, 10".

AS3 is _not_ a superset of ES3 as most people believe.

[1] <http://dmitrysoshnikov.com/ecmascript/the-quiz/>

[2] I have been working on an AS3 compiler lately, which is why I mixed JS and
AS3.

------
minikomi
From the answers to the first intermediate quiz:

    
    
        // These are functions
        // used as closures...
    
        // This is good
        (function() {
          var foo = 'bar';
        })();
    

This, to me, seems an odd use of the word closure. Since no value is "closed
over" with a function in the internal scope and preserved, it is simply an
anonymous function being immediately invoked.

That said, these quizzes are a good way to learn but man does JavaScript have
a lot of traps.

------
borplk
Second example, "function scope"

"Based on knowledge of several other languages (such as Java or C), this would
certainly be true. However, Javascript doesn't change scope when entering if
statements, loops, or anything like that, really."

Dude what the hell?

In Java and C, 'if' blocks don't have their own isolated scope either. The
equivalent Java or C code would also work perfectly fine.

~~~
dhconnelly
No, you're incorrect, C and Java do have block scope. It would have taken like
5 seconds to verify that. In C:

    
    
        #include <stdio.h>
        int main() {
            if (1) {
                int a = 5;
            }
            printf("%d\n", a);
            return 0;
        }
    

==> test.c:6: error: ‘a’ undeclared (first use in this function)

~~~
niggler
In older versions of C that was the only way to declare variables in a
function after the first non-declaration statement.

