Job Interview Questions

Discussion in 'Javascript' started by Chu, Jun 19, 2005.

  1. Chu

    Chu Guest

    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?
     
    Chu, Jun 19, 2005
    #1
    1. Advertising

  2. Chu

    Jim Ley Guest

    On 19 Jun 2005 14:16:43 -0700, "Chu" <> wrote:

    >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.
     
    Jim Ley, Jun 19, 2005
    #2
    1. Advertising

  3. Chu

    Kristian Thy Guest

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


    Flush the buffer, of course.

    --
    \\kristian
     
    Kristian Thy, Jun 19, 2005
    #3
  4. Chu

    Chu Guest

    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?
     
    Chu, Jun 19, 2005
    #4
  5. Chu

    Jim Ley Guest

    On 19 Jun 2005 21:39:04 GMT, Kristian Thy <> wrote:

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

    >
    >Flush the buffer, of course.


    And how do you do that exactly?

    Jim.
     
    Jim Ley, Jun 19, 2005
    #5
  6. Chu

    Ivo Guest

    "Chu" <> wrote
    > 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
     
    Ivo, Jun 19, 2005
    #6
  7. Chu

    Kristian Thy Guest

    Quoth Jim Ley:
    > On 19 Jun 2005 21:39:04 GMT, Kristian Thy <> wrote:
    >
    >>Quoth Chu:
    >>> 2) What is the main thing that has to be done when performing a
    >>> document.write() in a script block?

    >>
    >>Flush the buffer, of course.

    >
    > And how do you do that exactly?


    You start by adjusting your sarcasmometer ...

    --
    \\kristian
     
    Kristian Thy, Jun 19, 2005
    #7
  8. "Chu" <> writes:

    > 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
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jun 19, 2005
    #8
  9. Jim Ley wrote:
    > Chu wrote:
    >
    >>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.


    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.

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


    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.
     
    Richard Cornford, Jun 19, 2005
    #9
  10. Chu

    Csaba Gabor Guest

    Chu wrote:
    > 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
     
    Csaba Gabor, Jun 20, 2005
    #10
  11. Chu

    Matt Kruse Guest

    Chu wrote:
    > 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.

    --
    Matt Kruse
    http://www.JavascriptToolbox.com
    http://www.AjaxToolbox.com
     
    Matt Kruse, Jun 20, 2005
    #11
  12. Chu

    Lee Guest

    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.
     
    Lee, Jun 20, 2005
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MERIGON Olivier

    Job interview java technical questions

    MERIGON Olivier, Feb 15, 2004, in forum: Java
    Replies:
    6
    Views:
    5,713
    Scott Ellsworth
    Feb 17, 2004
  2. Gaurav

    Job interview questions

    Gaurav, Sep 16, 2007, in forum: C++
    Replies:
    10
    Views:
    577
    s. keeling
    Sep 21, 2007
  3. Vijay
    Replies:
    2
    Views:
    658
    Öö Tiib
    May 7, 2010
  4. reema
    Replies:
    0
    Views:
    309
    reema
    Aug 26, 2008
  5. Gaurav

    Job interview questions

    Gaurav, Sep 16, 2007, in forum: Javascript
    Replies:
    7
    Views:
    151
    s. keeling
    Sep 21, 2007
Loading...

Share This Page