Peter said:
When the question of seeking javascript tests on the interment comes up
my imagination usually conjures up an individual who thinks they will be
able to get away with using other people's code and copy-pasting their
way though life, if only they could get a foot through the door.
Preferably into a job where they were the only person creating the
javascript, and so not have anyone looking over their shoulders that
knew what they were doing. The good thing about such a position is that
the people doing the interview would not know what questions they should
be asking and so would likely get any technical test they used off the
Internet themselves. And so a thorough search for such tests, and the
rote learning of the 'correct' answers, might get them past the
technical test and into such a job.
But then I am very cynical.
I have the problem of setting javascript technical tests for interviews
(not that often, but it is part of my job), so form time to time I think
about what questions they should include, and what I would be looking
for in an answer. For example, as a verbal question I would tend to ask;
"which side of an inequality expression is evaluated first?" Not because
I want to be told the answer (I would not memorise that sort of detail
(after all, it does not matter 99.9% of the time), and it would scare me
to encounter someone who did), but because I would want to hear "I would
have to look it up" (or words to that effect), so I could ask where they
would look it up, and if given the correct answer hand them a copy of
the document to see if they were familiar enough with it (and
interpreting it) to give me the correct answer quickly.
On the other hand there are things I would expect someone to know
(without having to look it up), and one of those is Identifier
resolution against the scope chain.
"Inspired" by one of the more ambiguous questions on your meebo.com page
I thought the following might make quite interesting written test
questions, and give an impression of my thought process in setting
javascript questions:-
/* unknown global code */
function outerFunction(){
/* unknown outer function body code */
function innerFunction(){
/* unknown inner function body code */
with(anObjectReference){
x = 5; //<--- The subject line of code.
}
/* more unknown inner function body code */
}
/* more unknown outer function body code */
}
/* more unknown global code */
/* ********************************************************\
| Note: Three facts about the 'unknown' code:- |
| |
| 1. There are no more function definitions, no function |
| expressions and no uses of the Function constructor. |
| 2. There are no - with - statements in the unknown code.|
| 3. There are no uses of the - eval - function. |
\******************************************************** */
Q1: Assuming the line that reads - x = 5; - is executed, which (group
of) of the following are possible outcomes of its execution?
1. The creation of an 'x' property of the 'outerFunction' function
and the assignment of the value 5 to that property.
2. The assignment of the value 5 to a pre-existing 'x' property of
the 'outerFunction' function.
3. The creation of an 'x' property of the 'innerFunction' function
and the assignment of the value 5 to that property.
4. The assignment of the value 5 to a pre-existing 'x' property of
the 'innerFunction' function.
5. The creation of an 'x' property of the object referred to by
'anObjectReference' and the assignment of the value 5 to that
property.
6. The assignment of the value 5 to a pre-existing 'x' property of
the object referred to by 'anObjectReference'.
7. The creation of a local variable of the 'outerFunction' function
named 'x' and the assignment of the value 5 to that variable.
8. The assignment of the value 5 to a declared local variable of the
'outerFunction' function named 'x'.
9. The creation of a local variable of the 'innerFunction' function
named 'x' and the assignment of the value 5 to that variable.
10. The assignment of the value 5 to a declared local variable of the
'innerFunction' function named 'x'.
11. The creation of a global variable named 'x' and the assignment of
the value 5 to that variable.
12. The assignment of the value 5 to a declared global variable
named 'x'.
13. The creation of an 'x' property of the global object and the
assignment of the value 5 to that property.
14. The assignment of the value 5 to a pre-existing 'x' property of
the global object.
15. The creation of an 'x' property of the window object and the
assignment of the value 5 to that property.
16. The assignment of the value 5 to a pre-existing 'x' property of
the window object.
17. A runtime error.
Q2: If the line of code above is changed from - x = 5; - to - var x =
5 - which (group of) the above are then the possible outcomes of the
execution of that line?
I would have to go over the answers with the candidate taking the test
as there are a number of 'understandable mistakes' to be easily made
here (that is, getting some of them wrong is a certain fail, but others
may need the thinking behind the answer.)
Richard.