Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > JavaScript knowledge test

Reply
Thread Tools

JavaScript knowledge test

 
 
Peter Michaux
Guest
Posts: n/a
 
      08-01-2007
Hi Rob,

Not that I think I'm a qualified know-it-all but in the spirit of
discussion...

On Aug 1, 6:50 am, RobG <(E-Mail Removed)> wrote:

> > 2. The assignment of the value 5 to a pre-existing 'x' property of
> > the 'outerFunction' function.

>
> Possible, if anObjectReference does not have an x property.


I think the only way to set a prexisting x *property* of outerFunction
would be if anObjectReference refers to outerFunction. In this case
anObjectReference would have an x property.


> > 4. The assignment of the value 5 to a pre-existing 'x' property of
> > the 'innerFunction' function.

>
> Not possible, the innerFunction function object might have an x
> property, but it isn't on the scope chain. However, innerFunction's
> execution object (which is on the scope chain) may have an x property
> if one's been declared and that could be assigned a value.


If anObjectReference refers to the innerFunction object then the
properties of innerFunction are in the scope chain.


> > 17. A runtime error.

>
> Possible if anObjectReference isn't an object.


But this runtime error isn't due to the subject line of code. It is
due to the line before it.

Peter

 
Reply With Quote
 
 
 
 
Peter Michaux
Guest
Posts: n/a
 
      08-01-2007
On Jul 31, 10:42 pm, David Mark <(E-Mail Removed)> wrote:
> On Aug 1, 12:46 am, Peter Michaux <(E-Mail Removed)> wrote:
>
> > On Jul 31, 6:35 pm, David Mark <(E-Mail Removed)> wrote:

>
> > > On Jul 31, 7:43 pm, "Richard Cornford" >
> > > > 17. A runtime error.

>
> > > var anObjectReference;

>
> > > function outerFunction(){
> > > function innerFunction() {
> > > with(anObjectReference){
> > > x = 5; //<--- The subject line of code.
> > > }
> > > }
> > > innerFunction()
> > > }
> > > outerFunction();

>
> > I believe the error here isn't coming from the subject line of code
> > but the line before it.

>
> Correct. That's one I missed due to not following instructions to the
> letter.
>
>
>
> > A runtime error could still occur due to the subject line of
> > JavaScript. Taking it to the absurd, I have no clue what the
> > programmer who embedded JavaScript in a particular application has
> > done with the global x property. It may be that setting the global x
> > property always results in a runtime error.

>
> I don't quite follow you there.


I'm thinking of a non-browser embedding of JavaScript where the
programmer to whom I refer and who has made the embedding has created
a x property of the global object. This programmer can create a
genuine JavaScript runtime error for virtually anything. This isn't a
runtime error due to any of the code in the JavaScript engine (eg
spidermonkey) but it is a JavaScript runtime error nonetheless. Like I
said, this is a relatively absurd answer.


> I find no real joy in writing JavaScript or any other sort of code. I
> do enjoy designing and building applications (somewhat), but the
> actual coding is drudgery to me.


I can understand this sentiment at times However using higher order
functions and to dramatically reduce the amount of code I need to
write and maintain can be a real joy just for the ingenuity of it all.

Peter

 
Reply With Quote
 
 
 
 
Peter Michaux
Guest
Posts: n/a
 
      08-01-2007
On Jul 31, 4:43 pm, "Richard Cornford" <(E-Mail Removed)>
wrote:
>
> "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. |
> \************************************************* ******* */


I started thinking about strings in JavaScript that can be used like
eval() or the function constructor. I think that no use of
setTimeout() or setInterval() with a string first argument should be
added to the list of facts also.

 
Reply With Quote
 
dhtmlkitchen@gmail.com
Guest
Posts: n/a
 
      08-01-2007
On Jul 31, 5:21 pm, "Richard Cornford" <(E-Mail Removed)>
wrote:
> (E-Mail Removed) wrote:
> > On Jul 31, 8:29 am, Peter Michaux wrote:
> >> On Jul 31, 5:24 am, marss wrote:

>
> >>> Maybe anyone know good free online JavaScript knowledge
> >>> test? This not exactly a system for testing online
> >>> required - it may be simply list of questions with
> >>> variants of answers (I have to prepare tests for
> >>> learners and I need something to be taken as basis).

>
> >> > I was able to find only this (http://www.w3schools.com/js/
> >> > js_quiz.asp), but I need more.

>
> >>http://blog.meebo.com/?page_id=254

>
> >> Peter

>
> > Jeopardy style:

>
> > 1. b is a built-in object; not a String or string literal.
> > Define b.
> > if( b ) {
> > alert( "if: " + typeof b );
> > }
> > else {
> > alert( "else: " + b );
> > }
> > result: alerts "if: false"

>
> Given the definition of "built-in object" in ECMA 262, 3rd Ed. Section
> 4.3.7, and especially the words "Every built-in object is a native
> object", and the definition of - typeof - operator in section 11.4.3,
> where all 'native' objects must result in the strings 'object' or
> 'function' when used as a operand of - typeof -, are you sure you are
> not testing for knowledge of implementation bugs here?
>

Yep. It's pretty straightforward, although if you know of any
implementation bugs, please let us know.

Hint: What are the built-in objects? (process of elimination
strategy).

> > 2. Define p and q (they are not Strings). Result:
> > p < q; // false.
> > p <= q; // true.
> > p == q; // false.

>
> What about the execution order of these tests, as a strict adherence to
> the order shown greatly expands the possibilities?
>
> Richard.


Execution order is irrelevant, so you could have also:
p == q; // false
p <= q; // true
p < q; // false

However, if you do know a way in which execution order would make a
difference, please explain.
Hint: This is a test of how operators work.

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      08-01-2007
Richard Cornford wrote:
> "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.


Not possible. For the creation of a property of a Function object that
object needs to be referenced explicitly.

> 2. The assignment of the value 5 to a pre-existing 'x' property of
> the 'outerFunction' function.


Not possible. For the assignment to a property of a Function object
that object needs to be referenced explicitly.

> 3. The creation of an 'x' property of the 'innerFunction' function
> and the assignment of the value 5 to that property.


Same here.

> 4. The assignment of the value 5 to a pre-existing 'x' property of
> the 'innerFunction' function.


And here.

> 5. The creation of an 'x' property of the object referred to by
> 'anObjectReference' and the assignment of the value 5 to that
> property.


At least unlikely. The ambiguity in the `with' statement is that it
usually does not apply to assignment statements without a
VariableReference left-hand side. Which is why it is deprecated.

> 6. The assignment of the value 5 to a pre-existing 'x' property of
> the object referred to by 'anObjectReference'.


Same here.

> 7. The creation of a local variable of the 'outerFunction' function
> named 'x' and the assignment of the value 5 to that variable.


Not possible. For the creation of local variables requires a local
VariableStatement, however a VariableStatement would make the variable
one of innerFunction().

> 8. The assignment of the value 5 to a declared local variable of the
> 'outerFunction' function named 'x'.


Possible. The innerFunction() function must not have a local `x'
variable then.

> 9. The creation of a local variable of the 'innerFunction' function
> named 'x' and the assignment of the value 5 to that variable.


Not possible for the reasons given for 7.

> 10. The assignment of the value 5 to a declared local variable of the
> 'innerFunction' function named 'x'.


Possible.

> 11. The creation of a global variable named 'x' and the assignment of
> the value 5 to that variable.


Possible if neither innerFunction() nor outerFunction() have a variable
with that name declared.

> 12. The assignment of the value 5 to a declared global variable
> named 'x'.


Same here.

> 13. The creation of an 'x' property of the global object and the
> assignment of the value 5 to that property.


Possible. Global variables are properties of the Global Object, because
that is the Variable Object of the global execution context.

> 14. The assignment of the value 5 to a pre-existing 'x' property of
> the global object.


Possible if neither innerFunction() nor outerFunction() have a variable
with that name declared.

> 15. The creation of an 'x' property of the window object and the
> assignment of the value 5 to that property.


Possible, although the outcome is implementation- and context-dependent.
The host-defined `window' property of the Global Object may not refer
to the Global Object, of which the `x' property would be a property of
under the conditions mentioned for 14.

> 16. The assignment of the value 5 to a pre-existing 'x' property of
> the window object.


Same here.

> 17. A runtime error.


Possible if x was not declared before and x is an ID or a name of a DOM
object of the IE DOM (MSHTML component). I can also think of broken
implementations that would not allow a variable to have the same ID or
name as a DOM object.

> 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?


9 and 10.

> 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.)


I am looking forward to your evaluation of my answers.


Regards,
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      08-01-2007
On Aug 2, 2:56 am, Peter Michaux <(E-Mail Removed)> wrote:
> Hi Rob,
>
> Not that I think I'm a qualified know-it-all but in the spirit of
> discussion...
>
> On Aug 1, 6:50 am, RobG <(E-Mail Removed)> wrote:
>
> > > 2. The assignment of the value 5 to a pre-existing 'x' property of
> > > the 'outerFunction' function.

>
> > Possible, if anObjectReference does not have an x property.

>
> I think the only way to set a prexisting x *property* of outerFunction
> would be if anObjectReference refers to outerFunction. In this case
> anObjectReference would have an x property.
>
> > > 4. The assignment of the value 5 to a pre-existing 'x' property of
> > > the 'innerFunction' function.

>
> > Not possible, the innerFunction function object might have an x
> > property, but it isn't on the scope chain. However, innerFunction's
> > execution object (which is on the scope chain) may have an x property
> > if one's been declared and that could be assigned a value.

>
> If anObjectReference refers to the innerFunction object then the
> properties of innerFunction are in the scope chain.


My logic was faulty on both questions - I never considered that
anObjectReference might be to inner/outerFunction. So I got 2 "right"
by accident, and 4 wrong on purpose. I think the tedium got to me!


> > > 17. A runtime error.

>
> > Possible if anObjectReference isn't an object.

>
> But this runtime error isn't due to the subject line of code. It is
> due to the line before it.


Which means that the line itself is never executed - perhaps a
question along those lines would be more appropriate. It would also
be more difficult to fathom, most people are unlikely to consider
runtime errors as a strategy for preventing particular lines of code
from executing .


--
Rob

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      08-01-2007
RobG wrote:
> On Aug 2, 2:56 am, Peter Michaux <(E-Mail Removed)> wrote:
>> On Aug 1, 6:50 am, RobG <(E-Mail Removed)> wrote:
>>>> 2. The assignment of the value 5 to a pre-existing 'x' property of
>>>> the 'outerFunction' function.
>>> Possible, if anObjectReference does not have an x property.

>> I think the only way to set a prexisting x *property* of outerFunction
>> would be if anObjectReference refers to outerFunction. In this case
>> anObjectReference would have an x property.
>>
>>>> 4. The assignment of the value 5 to a pre-existing 'x' property of
>>>> the 'innerFunction' function.
>>> Not possible, the innerFunction function object might have an x
>>> property, but it isn't on the scope chain. However, innerFunction's
>>> execution object (which is on the scope chain) may have an x property
>>> if one's been declared and that could be assigned a value.

>> If anObjectReference refers to the innerFunction object then the
>> properties of innerFunction are in the scope chain.

>
> My logic was faulty on both questions - I never considered that
> anObjectReference might be to inner/outerFunction. [...]


However, Peter is mistaken here. The `with' statement does not apply to
the left-hand side of this assignment, no matter the object referenced
with anObjectReference. Unless x is declared, it will always reference
a property of the Global Object, that is a global variable. And so it
will either create that, modify it, or due to the peculiarities of an
execution environment cause a runtime error.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
 
Reply With Quote
 
Peter Michaux
Guest
Posts: n/a
 
      08-01-2007
On Aug 1, 2:04 pm, RobG <(E-Mail Removed)> wrote:
>
>
> > > > 17. A runtime error.

>
> > > Possible if anObjectReference isn't an object.

>
> > But this runtime error isn't due to the subject line of code. It is
> > due to the line before it.

>
> Which means that the line itself is never executed - perhaps a
> question along those lines would be more appropriate. It would also
> be more difficult to fathom, most people are unlikely to consider
> runtime errors as a strategy for preventing particular lines of code
> from executing .


Oddly enough . . . I've done this! I considered run-time errors due to
a browser bug/limitation to determine which versions of a particular
old browser could make an XHR POST. This was probably the weirdest
feature test I've ever made but avoided a use of navigator.userAgent.

// NN6.2 can't make POST requests because can't have arguments to
send()
// so now catch NN6.2 and any other browsers that can't take
argument to XHR.send()
function cannotPost() {
var xhr = new XMLHttpRequest();
try {
xhr.send("asdf");
} catch (e) {
// All calls to xhr.send() should error because there wasn't a
call to xhr.open()
// however the normal error is something about "not initialized"
as expected
// since xhr.open() was not called. NN6.2 gives a different
error indicating
// xhr.send() cannot take arguments.
if (-1 !== e.toString().indexOf("Could not convert JavaScript
argument arg 0 [nsIXMLHttpRequest.send]")) {
return true;
}
}
return false;
}


Peter

 
Reply With Quote
 
Peter Michaux
Guest
Posts: n/a
 
      08-01-2007
On Aug 1, 2:22 pm, Thomas 'PointedEars' Lahn <(E-Mail Removed)>
wrote:
> RobG wrote:
> > On Aug 2, 2:56 am, Peter Michaux <(E-Mail Removed)> wrote:
> >> On Aug 1, 6:50 am, RobG <(E-Mail Removed)> wrote:

>
> >>>> 4. The assignment of the value 5 to a pre-existing 'x' property of
> >>>> the 'innerFunction' function.


> >>> Not possible, the innerFunction function object might have an x
> >>> property, but it isn't on the scope chain. However, innerFunction's
> >>> execution object (which is on the scope chain) may have an x property
> >>> if one's been declared and that could be assigned a value.
> >> If anObjectReference refers to the innerFunction object then the
> >> properties of innerFunction are in the scope chain.

>
> > My logic was faulty on both questions - I never considered that
> > anObjectReference might be to inner/outerFunction. [...]

>
> However, Peter is mistaken here. The `with' statement does not apply to
> the left-hand side of this assignment, no matter the object referenced
> with anObjectReference. Unless x is declared,


Richard's #4 states that when the subject line executes that a
innerFunction.x does already exist.

> it will always reference
> a property of the Global Object, that is a global variable. And so it
> will either create that, modify it, or due to the peculiarities of an
> execution environment cause a runtime error.


I ran the following in Mac/Firefox2 and it indicates a "yes" to
Richard's statement.

function outerFunction() {
innerFunction.x = 2;
var anObjectReference = innerFunction;
function innerFunction() {
with (anObjectReference) {
x=5;
}
}
innerFunction();
alert(innerFunction.x); // 5
}
outerFunction();

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      08-01-2007
Peter Michaux wrote:
> | 4. The assignment of the value 5 to a pre-existing 'x' property of
> | the 'innerFunction' function.
>
> [...]
> I ran the following in Mac/Firefox2 and it indicates a "yes" to
> Richard's statement.
>
> function outerFunction() {
> innerFunction.x = 2;
> var anObjectReference = innerFunction;
> function innerFunction() {
> with (anObjectReference) {
> x=5;
> }
> }
> innerFunction();
> alert(innerFunction.x); // 5
> }
> outerFunction();


Confirmed for Firefox 2 on Windows XP. Thanks for surprising me.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
jQuery Selectors Quiz - Test Your Knowledge Garrett Smith Javascript 20 06-02-2010 04:23 PM
TEST TEST Test...Blah Blah Blah Generalbatguano@pacbell.net Computer Support 6 09-13-2006 01:53 AM
New: Javascript "Knowledge Base" search page Matt Kruse Javascript 6 04-14-2006 05:21 AM
TEST TEST TEST Gazwad Computer Support 2 09-05-2003 07:32 PM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments