Skip to content

Instantly share code, notes, and snippets.

@gillesruppert
Forked from rmurphey/screening.js
Created September 13, 2010 23:10
Show Gist options
  • Save gillesruppert/578232 to your computer and use it in GitHub Desktop.
Save gillesruppert/578232 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);
foo ? bar.doSomething(el) : bar.doSomethingElse(el); // prefer this as it reads better to me
// 2: what is the faulty logic in the following code?
var foo = 'hello';
(function() {
var foo = foo || 'world';
console.log(foo);
})();
// foo is scoped to the anonymous function and hence won't see the foo var is closes over
// 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();
foo.bar = 'new'; // override bar for foo only
Thinger.prototype.bar = 'both'; // override bar for all instances of Thinger
Thinger.prototype.toString = function () { // implement a toString method that returns value of bar
return this.bar;
};
foo.bar === Thinger.prototype.bar; // check whether object property is overridden
// 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()
};
for (var o in myObjects) {
if (myObjects.hasOwnProperty(o)) {
o.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' ];
// http://www.bbc.co.uk/glow/docs/1.7/api/glow.lang.shtml#staticmethod:map
var myNewArray = glow.lang.map(myArray, function (item) {
return Array(4).join((item + " "), ' ');
});
// 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();
});
// improved
$(document).ready(function() {
var $bar = $('.foo #bar'); // cache the selector
$bar.css({'color': 'red', 'border': '1px solid blue'})
.text('new text!')
.click(function() {
$(this).attr('title', 'new title')
.width('100px');
});
$bar.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
(function() {
var foo;
dojo.xhrGet({
url : 'foo.php',
load : function(resp) {
foo = resp.foo;
if (foo) {
// run this important code
}
}
});
})();
// since the xhr is asynchronous, we need to run the important code as part of the callback
// 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 selectors = ['foo', 'bar', 'baz', 'bop'];
for (var i = 0, l = selectors.length; i < l; i++) {
var s = selectors[i];
$('li.' + s + ' a').attr('title', 'i am ' + s);
}
})(dojo, dojo.query);
// 9: how would you improve the following code?
var thinger = '', gizmo = '';
for (i = 0; i <= 100; i++) {
thinger += '<p><span class="thinger">i am thinger ' + i + '</span></p>';
gizmo += '<p><span class="gizmo">i am gizmo ' + i + '</span></p>';
}
$('#thinger').append(thinger);
$('#gizmo').append(gizmo);
// 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;
}
// nothing checks whether the values provided are numbers, negative, percentages, etc
// 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'
}
];
http://www.bbc.co.uk/glow/docs/1.7/api/glow.lang.shtml#staticmethod:map
function stringifyMenu(menu) {
return glow.lang.map(menu, function(i) {
var s = i.name;
if (i.extras) {
s += ' (' + i.extras.join(', ') + ')';
}
return s;
});
}
stringifyMenu(menuItems);
// BONUS: write code such that the following alerts "Hello World"
say('Hello')('World');
function say(h) {
return function(w) {
return h + ' ' + w;
};
}
// 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(', '));
// month is 1 off & it does not respect month boundaries (i.e. it could do 8/33)
// date is in American format, mm/dd (no i18n)
var date = new Date(),
dates = [];
for (var i = 0; i <= 5; i++) {
var now = new Date(date);
now.setDate(date.getDate() + i);
dates.push((now.getMonth() + 1) + '/' + now.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