Skip to content

Instantly share code, notes, and snippets.

@pomeh
Forked from rmurphey/screening.js
Created September 13, 2010 21:02
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 pomeh/578048 to your computer and use it in GitHub Desktop.
Save pomeh/578048 to your computer and use it in GitHub Desktop.
// 1: how could you rewrite the following to make it shorter?
if (foo) {
bar.doSomething(el);
} else {
bar.doSomethingElse(el);
}
// ----------
bar[foo?'doSomething':'doSomethingElse'](el);
// 2: what is the faulty logic in the following code?
var foo = 'hello';
(function() {
var foo = foo || 'world';
console.log(foo);
})();
// ----------
// mixin global and local scope
// if you want a variable called foo inside the function (1),
// but with different value from the "foo='hello'" variable,
// you should passin this as argument as you can rename it then.
// instead, if you want to modify the initial foo variable (2),
// just remove the var keywork from "var foo = foo || 'world';"
// (1)
var foo = 'hello';
(function(globalFoo) {
var foo = globalFoo || 'world';
console.log(foo);
})(foo);
// (2)
var foo = 'hello';
(function() {
foo = foo || 'world';
console.log(foo);
})();
// 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? (1) how would you affect the value of the bar
// property for both foo and bim? (2) how would you add a method to foo and bim to
// console.log the value of each object's bar property? (3) how would you tell if
// the object's bar property had been overridden for the particular object? (4)
var Thinger = function() {
return this;
};
Thinger.prototype = {
bar : 'baz'
};
var foo = new Thinger(),
bim = new Thinger();
// ----------
// (1)
foo.bar = 'foo';
// (2)
foo.bar = bim.bar = 'bar';
// or run "Thinger.prototype.bar = 'foo'" before calling var foo = new Thinger();
// (3)
Thinger.prototype.log = function() {
console.log( this.bar );
};
// (4)
foo.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()
};
// ----------
var myObjects = {
thinger : new myApp.Thinger(),
gizmo : new myApp.Gizmo(),
widget : new myApp.Widget()
};
for(var elem in myObjects)
{
myObjects[elem].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' ];
// ----------
var myArray = [ 'foo', 'bar', 'baz' ],
set = [],
value;
for(var elem in myArray)
{
value = myArray[elem];
set.push( [value, value, value].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();
});
// ----------
jQuery(function($){
// we assume that our "bar" id is unique (and it should be),
// so we don't need the .foo selector anymore
var elem = $('#bar'),
event = 'click';
elem
// we could also use define a CSS class and call the addClass method
.css( {color:'red', border:'1px solid blue'} )
.text( 'new text!' )
.bind( event, function(){
// since our "elem" variable refers to a unique object,
// we can use it inside the event callback and save one query to the DOM
elem
.attr( 'title', 'new title' )
.width( '100px' )
;
})
.trigger( event )
;
});
// 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
}
})();
// ---------
if (foo) {/* */}
// when executing this statement, foo may be still undefined if the xhr request is slow,
// we should move this piece of code inside the load callback
load : function(resp) {
foo = resp.foo;
if( foo )
{
}
}
// 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);
// ----------
(function($){
// i've never use dojo before so please don't punch me too hard :)
var classes = 'foo bar baz bop'.split(' ');
dojo.forEach( $('li[class] a'), function(){
classes[this.className] && $(this).attr( 'title', 'i am ' + this.className );
});
// is there a way to do like with jQuery and pass in a function as
// argument to the attr method ?this would eliminate the forEach call
})(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>');
}
// ----------
var thinger = [],
gizmo = [];
for (i = 0; i <= 100; i++) {
thinger.push('<p><span class="thinger">i am thinger ' + i + '</span></p>');
gizmo.push('<p><span class="gizmo">i am gizmo ' + i + '</span></p>');
}
$('#thinger').append( thinger.join('') );
$('#gizmo').append( gizmo.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;
}
// ----------
// tip should be a non-numeric value, or worse it could be a negative number
function calculateTotal(baseTotal, tip, tax, fee) {
return baseTotal + Math.abs(tip) + tax + fee;
}
// and even if the other values are provided by the application
// how can we be sure that the function hasn't been overridden ?
// 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'
}
];
// ----------
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'
}
];
var set = [],
o;
for(var elem in menuItems)
{
o = menuItems[elem];
set.push( o.name + (o.extras && o.extras.length? '(' + o.extras.join(', ') + ')' : '') );
}
say('Hello')('World');
// BONUS: write code such that the following alerts "Hello World"
// ----------
function say(first){
return function(second){
alert( first + ' ' + second );
};
}
// 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(', '));
// ----------
// what appends if we are on 12/30 ?
// it will print " 12/30, 12/31, 12/32, 12/33, 12/34"
/*
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