Job Interview Questions

C

Chu

Recently in a job interview I was asked the following questions. I was
able to answer the first one, but the 2nd one still baffles me. I'm
curious if anyone can provide a little insite:

1) What two chars have to be escaped when performing a
document.write()?

I answered that quotes, both single and double need to be escaped when
they are supposed to be part of the output of a document.write
statement.

He seemed happy with that response.

2) What is the main thing that has to be done when performing a
document.write() in a script block?

I had no clue. The question seems pretty vague... I couldn't and still
can't think of what "has" to be done, besides the obvious of including
it inside a <script> tag, or adding content to what will be outputted
in the document.write.

Any ideas?
 
J

Jim Ley

Recently in a job interview I was asked the following questions. I was
able to answer the first one, but the 2nd one still baffles me. I'm
curious if anyone can provide a little insite:

1) What two chars have to be escaped when performing a
document.write()?

I answered that quotes, both single and double need to be escaped when
they are supposed to be part of the output of a document.write
statement.

There's lots of others too, newlines and other newline like things
from unicode for example, others depending on if the document.write is
inside an XML org SGML document, but that's probably what the
incompetent test setter was wanting, javascript tests are almost
universally appalling.
2) What is the main thing that has to be done when performing a
document.write() in a script block?

nothing, it's a gibberish question, the man is either completely
clueless, or trying to throw you to see your reaction.

Jim.
 
C

Chu

Flush the buffer??? I would thing that's all done automatically...
granted, maybe you are just messing around as the question itself has
no real answer?
 
I

Ivo

Chu said:
Recently in a job interview I was asked the following questions. I was
able to answer the first one, but the 2nd one still baffles me. I'm
curious if anyone can provide a little insite:

1) What two chars have to be escaped when performing a
document.write()?

I 'd have mentioned the ampersand instead of the other pair of quotes (not
used for delimiting the current string). Not directly sure why tho. Come to
think of it, what a silly question it is! Doesn't it depend on what you are
about to write? If the text includes an umlaut or a less-than character...
2) What is the main thing that has to be done when performing a
document.write() in a script block?

It 's suspicious that they 're so interested in document.write, which method
has always been a bit cumbersome and is not even available in the latest
doctypes. I guess he meant what has to be done is make sure that the
document isn't already closed, ie. you 're not overwriting the current page,
as that is the usual problem with document.write.

But really, these types of questions say more about the questioner than any
answer sais about the candidate. Not sure if I 'd be happy to work there.
Ivo
 
L

Lasse Reichstein Nielsen

Chu said:
Recently in a job interview I was asked the following questions.

That happens a lot these days, it seems.
1) What two chars have to be escaped when performing a
document.write()?

My immediate response would have been "what do you mean?"
I answered that quotes, both single and double need to be escaped when
they are supposed to be part of the output of a document.write
statement.

Only one of them, and they need to be escaped inside any string
delimited by the same kind of quote.

I would say "<" and "&", if they are to be put into HTML verbatim.
He seemed happy with that response.

It's one way to understand a vague question :)
2) What is the main thing that has to be done when performing a
document.write() in a script block?

Call "document.write"? Or my next response: "What *are* you talking
about?!?".
I had no clue. The question seems pretty vague...

That's an understatement :)

Going speculative (they might like that), I'd say that since he
explicitly mentions a script block (the two other options being
intrinsic event handlers and external script files, and I wouldn't
document.write inside an intrinisic event handler), it would have to
be the one thing that differs between scripts embedded in HTML and
scripts in external files: The need to escape the ETAGO token
("</" -> "<\/").

/L
 
R

Richard Cornford

Jim said:
There's lots of others too, newlines and other newline
like things from unicode for example, others depending on
if the document.write is inside an XML org SGML document,
but that's probably what the incompetent test setter was
wanting, javascript tests are almost universally appalling.

I also would have thought it was line terminators that _must_ be escaped
in a string literal within document.write (and any other string literal
as well) as a line terminator in a string literal is a syntax error.
There are 4 line terminators in ECMA 262. All other characters only need
escaping under some circumstances.
nothing, it's a gibberish question,

Well maybe they meant "</script>" as that would likely be problematic in
a string literal in a document.write statement within a SCRIPT element
on an HTML page. Though again it is more to do with string literals than
with document.write. And of course the sequence that should be escaped
is the CDATA terminating "</"
the man is either completely clueless,
or trying to throw you to see your reaction.

I wonder whether a clueless test setter, encountering a candidate who
evidently knew javascript better than them, would see it as in their
interest to employ them.

Richard.
 
C

Csaba Gabor

Chu said:
2) What is the main thing that has to be done when performing a
document.write() in a script block?

I don't know about main thing, but one thing to watch out for is that
when you start doing the document.write(), it obliterates the old
document (more or less). However, the script blocks and their local
variables are OK (I haven't actually looked at what specs might say on
this - so they may say otherwise). To that end, I would say that it is
advisable to constuct a single string that you are going to 'blat' as
the new document before the document.open or any writing. And I would
only do a single document.write() to be on the safe side.

Consider the following:
<body onLoad='selfAdjust()'>
<script type='text/javascript'>
function selfAdjust() {
alertPre = "old div had: ";
var div = document.getElementById('mydiv');
var preScript = "<body><script type='text/javascript'>alert('";
document.open();
var part2 = alertPre + div.innerHTML + "');<\/script></body>";
document.write(preScript + part2);
document.close();
}
</script>
<div id=mydiv>Hi Mom</div>
</body>

This will fail because alertPre is global (sitting off window) and
document.open() will wipe it out. If you have, instead, var alertPre =
"old div had: "; then that part won't throw an error.

There is second point of failure, and that is the issue of what is
happening with that div when you wipe out its (old) document. Actually
this does not fail in FF, presumably FF realizes you have a reference
to the object, even if it is somewhat orphan by this point. However,
in IE 6, an empty string is plunked in.

Csaba Gabor from Vienna
 
M

Matt Kruse

Chu said:
1) What two chars have to be escaped when performing a
document.write()?

Others have answered, but I would have said:

1) The quoted string character - either ' or "

2) Any character starting with a back-slash (\, \n, \t, etc)

Stupid question, though.
 
L

Lee

Chu said:
Recently in a job interview I was asked the following questions. I was
able to answer the first one, but the 2nd one still baffles me. I'm
curious if anyone can provide a little insite:

1) What two chars have to be escaped when performing a
document.write()?
2) What is the main thing that has to be done when performing a
document.write() in a script block?

If I had asked these questions, it would have been to hear you
discuss them, much as they've been discussed here, more than to
hear your guess as to what I might think the "main" thing was.

But if they consider there to be exactly two characters that
need to be escaped, I would say they're looking for " and \,
and I wonder if they might believe that "document.close()" is
always required.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top