Skip to content

Instantly share code, notes, and snippets.

@devongovett
Forked from rmurphey/screening.js
Created September 13, 2010 21:03
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 devongovett/578051 to your computer and use it in GitHub Desktop.
Save devongovett/578051 to your computer and use it in GitHub Desktop.
// 1: how could you rewrite the following to make it shorter?
foo ? bar.doSomething(el) : bar.doSomethingElse(el);
// 2: what is the faulty logic in the following code?
//you are creating a local version of foo instead of overwriting
//the global version. Thus, foo will always be equal to world.
//The problem is fixed below.
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? 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 = 'test'; // #1
Thinger.prototype.bar = 'test'; // #2 (without running #1 first)
Thinger.prototype.logBar = function() { // #3
console.log(this.bar);
}
foo.bar == Thinger.prototype.bar; // #4
// 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 prop in myObjects) {
myObjects[prop].destroy();
delete myObjects[prop];
}
// 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' ];
//assuming ES5 methods
var out = myArray.map(function(value) {
return Array(4).join(value + ' ').trim();
})
// 6: how could you improve the following code?
$(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?
//the important code will never run because ajax requests are asynchronous
(function() {
var foo;
dojo.xhrGet({
url : 'foo.php',
load : function(resp) {
if (foo = resp.foo) {
//run this important code
}
}
});
})();
// 8: how could you rewrite the following code to make it shorter?
(function(d, $){
'foo bar baz bop'.split(' ').forEach(function(val) {
$('li.' + val + ' a').attr('title', 'i am ' + val)
})
})(dojo, dojo.query);
// 9: how would you improve the following code?
//prevent all that pointless DOM manipulation!!
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?
//the entered tip might not be a number
//try converting it; show an error if not possible
function calculateTotal(baseTotal, tip, tax, fee) {
tip = parseFloat(tip);
if(isNaN(tip)) alert('you must enter a number'); //show error
return baseTotal + tip + tax + fee;
}
// 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'
}
];
menuItems.map(function(value) {
return value.name + (value.extras && value.extras.length ? ' (' + value.extras.join(', ') + ')' : '')
});
// BONUS: write code such that the following alerts "Hello World"
say('Hello')('World');
function say(str1) {
return function(str2) {
alert(str1 + ' ' + str2);
}
}
// BONUS: what is the faulty logic in the following code? how would you fix it?
//if you are looking for the *next* five days the current date should not be included,
//and since you are looking for five days, < should be used instead of <=.
var date = new Date(),
day = date.getDate() + 1,
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(', '));
@mathiasbynens
Copy link

FYI, $(document).ready(function() { }); (question 6) can be shortened to $(function() { });. Since the latter is much easier to type, I consider that to be an additional improvement. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment