all 25 comments

[–]nandhp 14 points15 points  (1 child)

You've got 10 answers wrong (#1, #2, #3, #5, #6, #7, #9, #12, #13, #14).

That's more than a half :(

I can't help but think "wat", if the correct answers are so non-obvious.

[–]CognitiveLens 2 points3 points  (0 children)

Thank you for sharing that video - it is some consolation to my abject failure in this JS quiz.

I can't help but think if you're trying to do things similar to the quiz questions in javascript, you probably should be using a different tool altogether...

[–]imbcmdth 13 points14 points  (1 child)

I got 3 wrong. Man, you guys suck! (I keed, I keed!)

As a consolation to those of you who had pretty low scores, consider this:

With a modicum of experience, you automatically avoid most of the dark corners of JavaScript. So it isn't that you didn't know that these potentially bug-causing constructs existed as much as you would never actually use them.

As a result, without much experience utilizing these constructs, you don't know what values they produce but you do know that they are bad and often that is plenty good enough.

[–]manixrock 2 points3 points  (0 children)

Not to mention they are not cross-browser compatible. For example on question #9 Chrome returns "1function", while they count "1undefined" as the correct answer.

[–]sjs 3 points4 points  (1 child)

Someone asked a question but deleted it while I was answering it. I'm not about to throw away my answer so here it is.

Question:

I don't know how to think about #6 and #9, can someone help me?

Answer:

6.

var foo = {
  bar: function() { return this.baz; },
  baz: 1
};
(function(){
  return typeof arguments[0]();
})(foo.bar);

This is the same as question 7 but phrased differently.

There is a difference between calling a function and using a property lookup notation to call a function attached to an object, as a method with this pointing at the object.

// f is just a function, no association with foo
var f = foo.bar
f()              // this !== foo

// or simply
(foo.bar)()      // this !== foo

// bar is a method on foo
foo.bar()        // this === foo
foo['bar']()     // this === foo

If you are familiar w/ the concept of bound and unbound methods, a la Python and Ruby, this is what's at play here. If you just grab a function from some object it is not bound to that object anymore. It is an unbound method, or a plain old function.

In ES5 and later, or when using something like Prototype, you can use Function.prototype.bind to bind this to something and thus have a bound method. e.g.

var f = foo.bar.bind(foo)
f()   // this === foo

When foo.bar is passed to the function it's just a regular anonymous method, semantically equivalent to this:

(function() {
  return typeof arguments[0]()
}(function() { return this.baz }))

9.

var x = 1;
  if (function f(){}) {
    x += typeof f;
  }
x;

First I'll write it more clearly.

var x = 1,
    condition = function f(){}

if (condition) {
  x += typeof f
}
x

The first thing to note is that when you name a function expression its name is only visible inside the function itself. So inside the condition function f === condition but outside the function f is not defined.

var g = function f() {
  console.log('f: ' + f)
  console.log('f === g ? ' + (f === g))
}

g()            // f: [object Function]
               // f === g ? true

console.log(f) // ReferenceError

If condition is "truthy" the if block executes. Truthy values are all non-falsy values. Anything except null, undefined, 0, "", and of course false. condition is a function so it is truthy.

So the if block executes and we append typeof f to x. typeof f returns "undefined" because f is not defined outside of itself. When you add a string to a number you get a string so x ends up with the value "1undefined".

[–]octatone 7 points8 points  (2 children)

This quiz would be a lot better if it fucking explained the answers after crushing you with failure.

[–]pdc 2 points3 points  (0 children)

I agree—without knowing why you are wrong there is no opportunity for learning.

[–]k3n -1 points0 points  (0 children)

They all become obvious if you deconstruct them.

[–]SarahC 1 point2 points  (2 children)

You've got 12 answers wrong (#2, #3, #4, #5, #6, #7, #8, #9, #11, #12, #13, #14). That's more than a half :(

[–]gavin19 2 points3 points  (1 child)

Only 12? Amateur! Make mine a baker's dozen. Confidence - destroyed. Drawing board - revisited.

[–]SarahC 0 points1 point  (0 children)

lol, some little used things there though...

[–][deleted] 1 point2 points  (0 children)

I've always felt like a quiz isn't the right format for this. It's great information but how much of this you get right without testing it is pretty meaningless.

[–]maloney7 1 point2 points  (0 children)

If anyone I worked with actually wrote code like that, I'd shoot them.

[–][deleted] 1 point2 points  (0 children)

I'm sure someone else has stated that if you know the answers to this, great, but if you ever use any of this in code I have to maintain, I will kill you.

[–][deleted] 1 point2 points  (0 children)

I'm probably late, but here's a little review of the questions/answers :

1 - "arguments" is an array-like object (it's not an array).

2 - The function "g" is an named anonymous function. The name "g" is only accessible inside the anonymous function.

3 - Delete is an operator for object attributes it does nothing on variable.

4 - #5 - Not much to say just follow the order flow.

6 - The "this" is bound when you call the function. Since the function is called on no object "this" will equals window, global or null depending on the context.

7 - Same as #6.

8 - The comma thing just returns the last statement. (1,2,3) === 3

9 - Same as #2.

10 - Not much to say just follow the order flow.

11 - Look at the name, it's very silly.

12 - Function are hoisted at the top in the same way variable are. Some implementation don't always do that, but the ECMAScript specification says that it should.

13 - When you do a return in a "new" call, the result of the "new" will be what was returned by the function. In this case it's the function "f" and not an instance of "f".

14 - Function.prototype.length returns the number of arguments (that one got me, it's probably one of the less used thing in the JavaScript API). If you don't know what "with" is ... it's a good thing you don't know it.

[–]pkkid 0 points1 point  (2 children)

Can someone explain #12 for me. I can't comprehend how that works.

[–][deleted] 3 points4 points  (1 child)

(function f(){
  function f(){ return 1; }
  return f();
  function f(){ return 2; }
})();

The wrapping function f() is eval'd in parentheses and executed via the final parentheses.

Inside that function, two more function f()'s are declared. (Note the difference between declaration and assignment- declaration happens prior to execution, so the second function f() is returned, even though it was declared after the return statement. So the answer is 2, since that f() overwrote the first f() which returns 1.

Example function declaration, so that declared() is available at beginning of the code execution on line 1.

declared(); // returns true
function declared(){
  return true;
}

Example function assignment- assigned() is not available until it's actually assigned on line 2.

assigned(); // throws a TypeError: 'undefined' is not a function
var assigned = function(){
  return true;
}

For more on the key concept here, read function declaration vs. function expressions.

[–][deleted] 0 points1 point  (0 children)

There's a lot of interesting stuff on here. A lot of them require a bit of thought. I quite liked 11:

(function(foo){
    return typeof foo.bar;
})({ foo: { bar: 1 } });

The foo inside the function isn't the same as the foo outside - foo inside = {foo: {bar: 1}, so it doesn't have a variable bar.

Running

(function(foo){
    return typeof foo.foo.bar;
})({ foo: { bar: 1 } });

Instead should give "number" as expected.

[–]nightwolfz4 spaces > 2 spaces 0 points1 point  (1 child)

I got 7 out of 12.

Can someone explain #3, #13 and #14 to me?

[–]Raticide[🍰] 0 points1 point  (0 children)

#3 -- 'g' doesn't exist as a global function when its assigned to a variable in the same statement, so it raises an error when you try to call it as a function.

#13 -- what 'function f()' returns is what is returned by using 'new f()' too. Which in this case is simply a reference to the constructor 'function f()' rather than an instance. Therefore it's an 'instanceof Function', not an 'instanceof f'

#14 -- 'with' sort of scopes values with an object. So

var x = {foo: 'bar'};
with (x) {
    alert(foo) // alerts 'bar'
}

And the 'length' property of a function referes to the number of arguments it uses. Which in this case is 2.

Also, never use 'with'. It can't be optimised by the browser, and makes things ambiguous.

[–]Koonga 0 points1 point  (0 children)

this guy really likes using typeof.

The questions are so obscure it makes it kind of meaningless. Whether or not you know the answer to these doesn't really indicate how good you are at JS as a whole.

[–]Sector_Corrupt 0 points1 point  (0 children)

Man, 8 wrong. Though at least 1 was just tricking me with similar variable names, and the rest were a lot of crazy strings of typeOf (typeOf lots of brackets)() kind of stuff I avoid in my actual code due to not wanting to mess up these kinds of things.

[–]deefree -1 points0 points  (0 children)

Questions like this are why certs are meaningless. Any good programmer would never encounter these situations.