JavaScript interview questions

Posted in 'General, JavaScript' by James on June 7th, 2011

The questions below were asked in a preliminary check before the actual interview. The role itself was for a “JavaScript Engineer”. This particular set of questions is from a job I applied to over a year ago. I’ve chosen to share them here because I think readers of this blog would benefit from knowing what kind of things are expected if you’re applying to a JavaScript-centric role.

  • What is the relationship between ECMAScript, Javascript and Jscript?

  • What core types are available in Javascript?

  • What do the following statements return and why?

       1:  parseInt("07");
       2:  parseInt("09");
       3:  parseInt("010");
       4:   
       5:  "1" + 2 + 3;
       6:  3 + 2 + "1";
       7:   
       8:  "1" == 1;
       9:  "1" === 1;
      10:  "1" == true;
      11:  "1" === false;
  • Alert the contents of the bar variable using the foo variable
  •    1:  var foo = "bar";
       2:  var bar = "foobar";
    • Alert the string “foobar” after a 10 second delay.

    • Implement Array.prototype.filter for IE.

    • Create a Person class with public/private/privileged members and methods.

    • Why does the statement 5 * 1.015 not return 5.075?

    • Replace the string "The quick brown fox jumps over the lazy dog" with the string "The1 quick2 brown3 fox4 jumps5 over6 the7 lazy8 dog9".

    • List several ways that you can communicate asynchronously with the server.

    • How would you embed this URL in a link inside an XHTML document?

       1:  http://www.google.com/search?hl=en&q=%22xhtml%22
    • How would you serve a StyleSheet only to IE users older than IE8?

    • Write a jQuery plugin to display a list of images in a Lightbox.

    What do you think of these questions? Fair?

    Related:
    Comments:

    TiTiJune 7th, 2011 at 9:46 pm

    #1.
    ECMAScript = Define various detailled specs
    Javascript = The language, general word
    Jscript = A Microsoft implementation

    (please don’t ask more :-p)

    #2.
    Boolean
    Number
    String
    Date
    Object?

    (after a rapid search it seems the 2 last are wrong)

    #3.

       1:  parseInt("07"); // -> 7
       2:  parseInt("09"); // -> 9? Hum i known there something with 08 / octal base
       3:  // I also know second parameter of parseInt (base) is very important, typically 10 for most conversions.
       4:  parseInt(”010”); // -> 10?[ok wrong for the 2 last again, and i really don't care why.
       5:  I just know that it works if you set the second argument to 10. Fuck it. Damn binary-octal-stupid parser^^]
       6:   
       7:  "1" + 2 + 3; // -> "123" because of concatenation and `type priority` ; "1" + 2 = "12" ; "12" + 3 = "123"
       8:  3 + 2 + "1"; // -> "51" ; 3 + 2 = 5 ; 5 + "1" = "51"Do I just invented `type priority` ? Anyway i think i’m right even if i don’t know the real name used for the inner workings.
       9:   
      10:  "1" == 1; // -> true
      11:  "1" === 1; // -> false (value & type checked)
      12:  "1" == true; // -> true (because "1" == 1)
      13:  "1" === false; // -> false

    #4.

       1:  alert(eval(foo)); // eval is evil
    #5.
       1:  setTimeout(function()
       2:  {
       3:      alert("foobar");
       4:  }, 10000); // after a minimum of 10 seconds [likely a few ms after 10s passed]

    #6.

    uh I don’t really know it doesn’t exist in IE, I’m relying on higher level frameworks usually….
    I guess something like (sorry, comment system broke the indentation):

       1:  if(!Array.prototype.filter)
       2:  {
       3:      Array.prototype.filter = function(search)
       4:      {
       5:          var matches = [];
       6:          for(var i = 0, len = this.length; i<len; i++) // yeah i know while(i--) stuff is better, also a little more obscure
       7:          {
       8:              if(this[i].toString().indexOf(search) != -1) // toString(), nope?
       9:              {
      10:                  matches.push(this[i]);
      11:              }
      12:          }
      13:          return matches;
      14:      }
      15:  }
      16:   
      17:  // Please don't hit me if this is a bad implementation, but help me with a better one (and comments!).

    #7.

       1:  // This one is &quot;easy&quot; for me because i worked the question a few months back
       2:  // It&#039;s think it&#039;s an important question and the following model is not enough mentionned
       3:   
       4:  function Person()
       5:  {
       6:      this.publicMember = "foo";
       7:   
       8:      this.privilegedMethod = function()
       9:      {
      10:          return privateMethod();
      11:      }
      12:   
      13:      var privateMember = "bar";
      14:   
      15:      function privateMethod()
      16:      {
      17:          return "hello";
      18:      }
      19:   
      20:  }
      21:  // Public methods
      22:  Person.prototype = 
      23:  {
      24:      getDate: function()
      25:      {
      26:          return new Date();
      27:      },
      28:      getWorld: function()
      29:      {
      30:          return "world";
      31:      }
      32:  }
      33:  // Bonus: Public static method
      34:  Person.kill = function()
      35:  {
      36:      return "dead";
      37:  }
      38:   
      39:   
      40:  // (I think the closure way to get private stuff is more known, but I prefer this one :-p)
      41:  // And I guess questions on .prototype, performance, closure and stuff like that could be interesting.

    #8.

    1: 5 * 1.015 != 5.075 because of the way floats are stored internally (?+?+mantisse). Its an approximation. Leads to

    small variations when doing maths. Be really really careful for financial calculations!!

       1:  5 * 1.015 // 5.074999999999999
       2:  parseFloat((5 * 1.015).toFixed(3)) // 5.075
       3:  parseFloat(Math.round((5 * 1.015) * Math.pow(10,3)) / Math.pow(10,3)) // 5.075

    #9.

    *Challenge accepted*

       1:  //First draft:
       2:   
       3:  var tab = "The quick brown fox jumps over the lazy dog".split('');
       4:  var out = "";
       5:  for(var i = 0, len = tab.length; i<len; i++)
       6:  {
       7:      out += tab[i] + (i+1);
       8:  }
       9:  out; // WRONG: forgot spaces
      10:   
      11:   
      12:  //Second draft:
      13:   
      14:  var space = " ";
      15:  var tab = "The quick brown fox jumps over the lazy dog".split(space);
      16:  var out = "";
      17:  for(var i = 0, len = tab.length; i<len; i++)
      18:  {
      19:      out += tab[i] + (i+1) + space;
      20:  }
      21:  out; // WRONG: one more space at the end
      22:   
      23:   
      24:  //Third draft:
      25:   
      26:  var tab = "The quick brown fox jumps over the lazy dog".split(" ");
      27:  for(var i = 0, len = tab.length; i<len; i++)
      28:  {
      29:      tab[i] += (i + 1);
      30:  }

    31: tab.join(" "); // RIGHT, and elegant enough for me

       1:  //Fourth draft
       2:  var i = 0;
       3:  "The quick brown fox jumps over the lazy dog with a spoiler".replace(/ |$/g, function(a) {
       4:    return ++i + a;
       5:  });
       1:  // Fifth draft
       2:  var statement = "The quick brown fox jumps over the lazy dog";
       3:  var statement_new = statement.split(' ').map(function(word, index) {
       4:    return word + index;
       5:  }).join(' ');

    Dammit I forgot the replace method could take a function as second argument.
    Hum wondering about perfs… Depending on string length, targetted browsers, …

    #10.

    -AJAX (XmlHttpRequest)
    -Using flash & communication between flash and your javascript & flash remote connection

    // And thoses which look terrible to me
    -Creating a new element such as ,,
    -Changing an iframe src
    -AJAX with long polling (for server -> client)
    [-page reload / change page location]

    #11.

       1:  http://www.google.com/search?hl=en&amp;q=%22xhtml%22

    #12.

    [meh, comment system is really broken on this one ; had to use &lt; and &gt; by myself]

    <!– if [ IE < 8 ] –>
    <style type="text/css" src="iefail.css" />
    <!– [ endif ] –>

     

    (i'm sure this is not exactly thoses chars but the idea is here)
    (by the way i prefer NOT to use a separate stylesheet, also might be useful in some radical design decision ; and what about server sniffing to provide a different ?)

    #13.

    (function($)
    {
    	$.fn.lightBox = function()
    	{
    		//...
    		return this.each(function()
    		{
    			var $this = $(this);
    			$this.css('border', '1px solid red');
    			$this.click(function()
    			{
    				//...
    			});   //....
    		});	
    	}
    })(jQuery)
     
    //or Don’t reinvite the mass produced wheel 
     
    posted @ 2011-07-24 14:07  像阳光一样  阅读(933)  评论(0编辑  收藏  举报