Skip to content

Instantly share code, notes, and snippets.

@CrabDude
Forked from rmurphey/screening.js
Created September 14, 2010 00:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CrabDude/578299 to your computer and use it in GitHub Desktop.
Save CrabDude/578299 to your computer and use it in GitHub Desktop.
// NOT AN APPLICATION, I JUST WANTED TO TAKE YOUR TEST. =)
// 1: how could you rewrite the following to make it shorter?
if (foo) {
bar.doSomething(el);
} else {
bar.doSomethingElse(el);
}
// ANSWER
bar[foo ? 'doSomething' : 'doSomethingElse'](el);
//OR
bar['doSomething' + (foo ? '' : 'Else')](el);
// 2: what is the faulty logic in the following code?
var foo = 'hello';
(function() {
var foo = foo || 'world';
console.log(foo);
})();
// ANSWER
// The local instance of foo is instantiated prior to the evaluation of
// "foo || 'world'" thus foo is always undefined regardless.
// 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();
// ANSWER
// Override foo.bar w/o affecting bim.bar
foo.bar = 'new';
// Override foo.bar and bim.bar simultaneously
Thinger.prototype.bar = 'new';
// Add method to foo & bim to console.log this.bar
Thinger.prototype.log = function() {
console.log(this.bar);
};
// Determine if bar's property has been overridden
Thinger.prototype.isChanged = function() {
return this.bar != Thinger.prototype.bar;
};
// 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()
};
// ANSWER
for(var i in myObjects) {
myObjects.hasOwnProperty(i) ? myObjects[i].destroy() : null;
}
// 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' ];
// ANSWER
var newArray = $.map(myArray,function(v) {
return [v,v,v].join(' ');
});
// 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();
});
// ANSWER
$(function() {
$('.foo #bar').css({'color': 'red', 'border': '1px solid blue'}).
text('new text!').
click(function() {
$(this).attr('title', 'new title').
width('100');
}).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) {
// run this important code
}
})();
// ANSWER
// The important code will never be ran and likely belongs in the load callback
(function() {
var foo;
dojo.xhrGet({
url: 'foo.php',
load: function(resp) {
foo = resp.foo;
important();
}
});
function important() {
if (foo) {
// run this important code
}
}
})();
// OR depending on the context
(function() {
dojo.xhrGet({
url: 'foo.php',
load: function(resp) {
important(resp.foo);
}
});
function important(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);
// ANSWER
(function(d, $){
var i=-1, cls = ['foo','bar','baz','bop'];
while(++i < 4)
$('li.' + cls[i] + ' a').attr('title', 'i am ' + cls[i]);
})(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>');
}
// ANSWER
var tCont= [], gCont = [],
tSpan = ['<p><span class="thinger">i am thinger ','</span></p>'];
gSpan = ['<p><span class="gizmo">i am gizmo ','</span></p>'];
for (var i = 0; i <= 100; ++i) {
tCont.push(tSpan.join(i));
gCont.push(gSpan.join(i));
}
$('#thinger').append(tCont.join(''));
$('#gizmo').append(gCont.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;
}
// ANSWER
// NaN param values will result in NaN, concatenation could occur and would occur
// if tip is passed straight from text box
// Floating point errors
// 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'
}
];
// ANSWER
var newArray = $.map(menuItems,function(v) {
return v.name+($.isArray(v.extras) ? ' ('+v.extras.join(', ')+')' : '');
});
// BONUS: write code such that the following alerts "Hello World"
say('Hello')('World');
//ANSWER (I love Fab.js =)
function say(it) {
return function(why) {
alert(it+' '+why);
}
}
// BONUS: what is the faulty logic in the following code? how would you fix it?
var next, dates = [],
date = new Date(2010, 10, 30);
for (var i = 0; i <= 5; ++i) {
next = new Date(date);
next.setDate(date.getDate()+i);
dates.push(next.getMonth()+'/'+next.getDate());
}
console.log('The next five days are ', dates.join(', '));
/*
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