javascript quiz. questions to ask when recruiting developers

22
Javascript Quiz Technical questions to ask when recruiting developers. Alberto Naranjo Jan. 2014

Upload: alberto-naranjo

Post on 20-May-2015

752 views

Category:

Business


5 download

DESCRIPTION

A quick selection of easy/medium difficulty questions to ask when recruiting potential javascript developers.

TRANSCRIPT

Page 1: Javascript quiz. Questions to ask when recruiting developers

Javascript QuizTechnical questions to ask when recruiting developers.

Alberto Naranjo Jan. 2014

Page 2: Javascript quiz. Questions to ask when recruiting developers

OOP

Page 3: Javascript quiz. Questions to ask when recruiting developers

What’s prototyping?Javascript does’nt use classical ‘inheritance’ model. It uses prototypal inheritance. We will want to declare methods on the prototype class, not in the constructor, mainly because when the use new the object would create each time a new copy of the methods instead of using the common one from the prototype.

//Guitar function constructor function Guitar(color, strings) {

this.color = color; this.strings = strings;

}//Guitar prototype methodGuitar.prototype.play = function(chord){

return chord;}var myguitar = new Guitar(‘blue’,[‘A’,’F’,’G’]);---------Guitar.prototype = {

play : function(chord){ return chord; },getColor : function(){ return this.color; }

};

Page 4: Javascript quiz. Questions to ask when recruiting developers

How to create objects with properties? Also add a function as

a property.

var man = new Object();man.name = ‘Alberto Naranjo’;man.getName = function(){ return this.name; }console.log(man.getName()); //logs Alberto Naranjo

Page 5: Javascript quiz. Questions to ask when recruiting developers

Implement dot and literals object notation. What’s the

difference?

There is no practical difference.

var man = new Object();man.name = “Albert”; // man[‘name’] = “Albert”;man.age = 29; // man[‘age’] = 29;----------var man = { ‘name’ : “Andrew”, ‘age’ : 27 };

Page 6: Javascript quiz. Questions to ask when recruiting developers

Inheritance, how can you do it in JS? :)

Simple guide to inheritance: http://phrogz.net/JS/classes/OOPinJS2.html

Cat.prototype = new Mammal(); //Inheritance occurs  Cat.prototype.constructor = Cat; //Override new constructor  function Cat(name){ this.name=name; } //New constructor//We can override any method, and inherit old methods.

Page 7: Javascript quiz. Questions to ask when recruiting developers

General Syntax

Page 8: Javascript quiz. Questions to ask when recruiting developers

What’s event bubbling and event propagation. How to stop

propagation?Event bubbling describe the behavior of events in child and parents nodes in the Document Object Model. The child pass their events to their parents nodes. The main benefit of this behavior is the speed because the code has to traverse the DOM tree only once. And simplicity because you only need one event listener for all children nodes. For example, a click event listener in page’s body element, will trigger on any click of the inner components. Event capturing also called bubble down. Where outer elements events trigger before inner (parents before children).event.stopPropagation();event.cancelBubble = true; //for IE<9

Page 9: Javascript quiz. Questions to ask when recruiting developers

Implement dynamic function calling using dynamic

parameters.

var myDynamicFunc = (function(text){ alert(text); })(‘Hello!’);

Page 10: Javascript quiz. Questions to ask when recruiting developers

What’s a closure? Implement an example.

A closure is an inner function with 3 scopes: local variables, outer variables and global variables.

function showName (firstName, lastName) {

var nameIntro = "Your name is "; //this inner function has access to the outer function's variables, including params

function makeFullName () {

return nameIntro + firstName + " " + lastName;} return makeFullName ();

} showName ("Michael", "Jackson"); // Your name is Michael Jackson

Page 11: Javascript quiz. Questions to ask when recruiting developers

Explain differences between == and ===. Implement an

example.Briefly == will only check for the value, and === (strict equality) will check also for the type/object without type conversion. When comparing objects === will return false if they are not the same pointer/reference to the same object even if the have the same value.

object1 = new Number(‘10’);object2 = new Number(‘10’);object3 = object2;console.log(object1 === object2); //falseconsole.log(object2 === object3); //true

Page 12: Javascript quiz. Questions to ask when recruiting developers

Global vs local variable definition. Implement

both.Related to the scope, a global variable has no scope and it’s available on any place of the code. Good programmer should avoid it in all situations. A local variable has a local scope, inside a object, block or structure.

globalvar=true;var localvar=true;

Page 13: Javascript quiz. Questions to ask when recruiting developers

How the this keyword works?

In Javascript the this keyword usually references the object who owns the method. But depending on the scope. Sometimes you use this in reference to the Window object. When working with event handlers this references the object who created the event.

Page 14: Javascript quiz. Questions to ask when recruiting developers

How do you do error handling in JS?

Implement.You can use the structure try-catch-finally to manage the error handling.

try {//do something.

} catch(e) {console.log(e.message);document.write ("Error Message: " + e.message);document.write ("<br />");document.write ("Error Code: ");document.write (e.number & 0xFFFF);document.write ("<br />");document.write ("Error Name: " + e.name);} finally { //do something always.}

Page 15: Javascript quiz. Questions to ask when recruiting developers

Enumerate all Javascript types.

1. Number

2. Boolean

3. String

4. Object

5. function

6. null

7. undefined

Page 16: Javascript quiz. Questions to ask when recruiting developers

How timers work? What you should be aware of?

They run in a single thread so there would be events in queue.

setTimeout(function, miliseconds);-------var id = setInterval(function, miliseconds);clearInterval(id);

Page 17: Javascript quiz. Questions to ask when recruiting developers

How do you read or modify any property from a DOM

element?

var myProperty = document.getElementById(‘id’).property;document.getElementById(‘id’).value = ‘Hello!’;

Page 18: Javascript quiz. Questions to ask when recruiting developers

Arrays

Page 19: Javascript quiz. Questions to ask when recruiting developers

Implement a simple array with 3 elements

var myArray = new Array(‘a’,’b’,’c’);

Page 20: Javascript quiz. Questions to ask when recruiting developers

Implement an associative array.

I will use a literal object notation to create one.

var myArray={key1: 'value1', key2:'value2' };

alert(myArray[‘key1’]); // Also myArray.key1

Page 21: Javascript quiz. Questions to ask when recruiting developers

There is such for-each block in Javascript?

There is one, but it’s not fully supported. You can use also for-in structure.

a.forEach( function(entry) { console.log(entry); });-------var key;for (key in a) {console.log(a.key);}

Page 22: Javascript quiz. Questions to ask when recruiting developers

Suggest more questions ;)