Skip to content

Instantly share code, notes, and snippets.

@benhodgson
Forked from rmurphey/screening.js
Created September 14, 2010 11:35
Show Gist options
  • Save benhodgson/578901 to your computer and use it in GitHub Desktop.
Save benhodgson/578901 to your computer and use it in GitHub Desktop.
// 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<n; j++) list.push(word);
result.push(list.join(' '));
}
return result
};
repeatArray(myArray, 3);
// 6: how could you improve the following code?
$(document).ready(function() {
$('.foo #bar').css('color', 'red');
$('.foo #bar').css('border', '1px solid blue');
$('.foo #bar').text('new text!');
$('.foo #bar').click(function() {
$(this).attr('title', 'new title');
$(this).width('100px');
});
$('.foo #bar').click();
});
// A: chain jQuery method calls and combine CSS values into a single object
$(document).ready(function() {
$('.foo #bar').css({'color': 'red', 'border': '1px solid blue'});
.text('new text!');
.click(function() {
$(this).attr('title', 'new title').width('100px');
})
.click();
});
// 7: what issues do you see with the following code? how would you fix it?
(function() {
var foo;
dojo.xhrGet({
url : 'foo.php',
load : function(resp) {
foo = resp.foo;
}
});
if (foo) { // <----- this is always undefined; the xhrGet call is async
// run this important code
}
})();
// A:
(function() {
dojo.xhrGet({
url : 'foo.php',
load : function(resp) {
var foo = resp.foo;
if (foo) {
// run this important code
}
}
});
})();
// 8: how could you rewrite the following code to make it shorter?
(function(d, $){
$('li.foo a').attr('title', 'i am foo');
$('li.bar a').attr('title', 'i am bar');
$('li.baz a').attr('title', 'i am baz');
$('li.bop a').attr('title', 'i am bop');
})(dojo, dojo.query);
// A:
(function(d, $){
var v = ['foo', 'bar', 'baz', 'bop'];
for (var x in v) {
$('li.'+v[x]+' a').attr('title', 'i am '+v[x]);
}
})(dojo, dojo.query);
// 9: how would you improve the following code?
for (i = 0; i <= 100; i++) {
$('#thinger').append('<p><span class="thinger">i am thinger ' + i + '</span></p>');
$('#gizmo').append('<p><span class="gizmo">i am gizmo ' + i + '</span></p>');
}
// 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('<p><span class="thinger">i am thinger ' + i + '</span></p>');
gizmoVals.push('<p><span class="gizmo">i am gizmo ' + i + '</span></p>');
}
$('#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 <sam@hocevar.net>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment