// Hello! I am Ben Hodgson.
// 1: how could you rewrite the following to make it shorter?
if (foo) {
bar.doSomething(el);
} else {
bar.doSomethingElse(el);
}
// A:
bar[foo ? 'doSomething' : 'doSomethingElse'](el);
// 2: what is the faulty logic in the following code?
var foo = 'hello'; // this foo...
(function() {
var foo = foo || 'world'; // ...is not the same foo as this foo
console.log(foo);
})();
// (you redefine it in this self–invoking function's scope)
// 3: given the following code, how would you override the value of the bar
// property for the variable foo without affecting the value of the bar
// property for the variable bim? how would you affect the value of the bar
// property for both foo and bim? how would you add a method to foo and bim to
// console.log the value of each object's bar property? how would you tell if
// the object's bar property had been overridden for the particular object?
var Thinger = function() {
return this;
};
Thinger.prototype = {
bar : 'baz'
};
var foo = new Thinger(),
bim = new Thinger();
// 4: given the following code, and assuming that each defined object has a
// 'destroy' method, how would you destroy all of the objects contained in the
// myObjects object?
var myObjects = {
thinger : new myApp.Thinger(),
gizmo : new myApp.Gizmo(),
widget : new myApp.Widget()
};
// A:
for(var prop in myObjects) {
myObjects[prop].destroy();
}
// 5: given the following array, create an array that contains the contents of
// each array item repeated three times, with a space between each item. so,
// for example, if an array item is 'foo' then the new array should contain an
// array item 'foo foo foo'. (you can assume the library of your choice is
// available)
var myArray = [ 'foo', 'bar', 'baz' ];
// A:
function repeatArray(theArray, n) {
var result = [],
list,
word;
for(var i in theArray) {
list = [],
word = theArray[i];
for(var j=0; j
i am gizmo ' + i + '
'); } // A: manipulating the DOM is expensive. using join('') is also more memory- // efficient that concatenating strings in a compound fashion var thingerVals = [], gizmoVals = []; for (i = 0; i <= 100; i++) { thingerVals.push('i am thinger ' + i + '
'); gizmoVals.push('i am gizmo ' + i + '
'); } $('#thinger').append(thingerVals.join('')); $('#gizmo').append(gizmoVals.join('')); // 10: a user enters their desired tip into a text box; the baseTotal, tax, // and fee values are provided by the application. what are some potential // issues with the following function for calculating the total? function calculateTotal(baseTotal, tip, tax, fee) { return baseTotal + tip + tax + fee; } // A: If the tip value is read straight from a text box, it'll be a string // and so will just be concatenated to the string representation of // baseTotal. This is toxic: as string+number results in a string, the // concatenation will continue for tax and fee. This can be mitigated by // sanatising tip with parseInt(tip, 10) // 11: given the following data structure, write code that returns an array // containing the name of each item, followed by a comma-separated list of // the item's extras, if it has any. e.g. // // [ "Salad (Chicken, Steak, Shrimp)", ... ] // // (you can assume the library of your choice is available) var menuItems = [ { id : 1, name : 'Salad', extras : [ 'Chicken', 'Steak', 'Shrimp' ] }, { id : 2, name : 'Potato', extras : [ 'Bacon', 'Sour Cream', 'Shrimp' ] }, { id : 3, name : 'Sandwich', extras : [ 'Turkey', 'Bacon' ] }, { id : 4, name : 'Bread' } ]; // A: (the resulting array is stored in result) var result = [], item; for(var i in menuItems) { item = menuItems[i]; var itemText = item.name; if(item.extras && item.extras.length >0) { itemText += ' (' + item.extras.join(', ') + ')' } result.push(itemText); } // BONUS: write code such that the following alerts "Hello World" say('Hello')('World'); // A: function say(a) { return function(b) { alert(a + ' ' + b); } } // BONUS: what is the faulty logic in the following code? how would you fix it? var date = new Date(), day = date.getDate(), month = date.getMonth(), dates = []; for (var i = 0; i <= 5; i++) { dates.push(month + '/' + (day + i)); } console.log('The next five days are ', dates.join(', ')); // A: firstly, getMonth is zero-indexed, so it won't return the value that // is commonly used to number the month. Also, this code breaks on any day // less than five days before the end of the month, as it simply increments // the day (i.e. 8/29, 8/30, 8/31, 8/32, 8/33) /* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004 Copyright (C) 2004 Sam Hocevar