Skip to content

Instantly share code, notes, and snippets.

@banderson
Forked from rmurphey/screening.js
Created September 20, 2010 12: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 banderson/587794 to your computer and use it in GitHub Desktop.
Save banderson/587794 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);
}
// answer
(foo)
? bar.doSomething(el)
: bar.doSomethingElse(el);
// 2: what is the faulty logic in the following code?
var foo = 'hello';
(function() {
var foo = foo || 'world';
console.log(foo);
})();
// answer: Are you looking for something like: *appending* 'world' to the foo in global scope instead of conditionally overwriting it? (i.e. by taking it as a parameter to the function)
// Or that re-declaring foo in the function scope permanently 'hides' the global 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();
// answers
foo.bar = 'new'; // #1
Thinger.prototype.bar = 'newer'; // #2
// #s 3 and 4
Thinger.prototype.logValue = function() {
console.log(this.bar);
}
Thinger.prototype.propertyChanged = function(propName) {
return this[propName] !== Thinger.prototype[propName];
}
// 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 each (obj in myObjects) {
if (typeof obj === 'object')
obj.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' ];
// answer
var newArray = [];
for each (el in myArray) {
newArray.push(el +' '+ el +' '+ el);
}
// 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
jQuery(document).ready(function($) { // use 'jQuery' in global scope, pass in $ for local use
var bar = $('.foo #bar'); // cache jQuery object
bar.css({
'color': 'red',
'border': '1px solid blue'
})
.text('new text!')
.click(function() {
var $this = $(this); // cache
$this.attr('title', 'new title');
$this.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: There is a race condition. "if (foo) {..." should only fire after the XHR request is completed and assigns its output to foo, but it will run immediately after the request is *dispatched*.
// To fix this, you should put the 'if (foo) {...' inside the callback function, or alternatively fire a custom event and add this code to a function bound to that event (not sure how this works in dojo, however).
// 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?? (with some assumptions, not tested)
(function(d, $){
$('li a').each(function(element) { // assuming we want all links under li's, not just those 4 classes
$(element).attr('title', function(index, attr) {
return 'i am '+ $(element).attr('class'); // assuming each link only has one class
})
});
})(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: I'm not sure if this is faster than fragment caching with jQuery, but it's a start
var thinger = '',
gizmo = '',
tpl = '<p><span class="{{type}}">I am a {{type}} {{index}}</span></p>'; // poor man's templating
for (i = 0; i <= 100; i++) {
thinger += tpl.replace(/{{type}}/g, 'thinger').replace(/{{index}}/, i);
gizmo += tpl.replace(/{{type}}/g, 'gizmo').replace(/{{index}}/, i);
}
// limit DOM mods
$('#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;
}
// answer: the tip input will likely be a string unless there is some parsing that we don't see here. You'd need to wrap the tip var in parseFloat(...) to make sure it's handles as a number
// return baseTotal + parseFloat(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'
}
];
// answer:
var arr = [];
for (var i=0; i < menuItems.length; i++) {
var item = menuItems[i],
output = item.name;
if (item.extras) {
output += ' ('+ item.extras.join(', ') +')';
}
arr[arr.length] = output;
}
// BONUS: write code such that the following alerts "Hello World"
say('Hello')('World');
// answer:
function say(phrase) {
return function(anotherPhrase) {
alert(phrase + ' ' + anotherPhrase);
}
}
// 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(', '));
// answer: months are zero-based, need to add 1 (#1),
// the loop should start at 1 so tomorrow is the first day in the list (#2),
// AND only get the next 5 days, not six (by switching loop to i <= 5) (#3)
var date = new Date(),
dates = [];
for (var i = 1; i <= 5; i++) { // #s 2 & 3
date.setDate(date.getDate()+1); // increment date by 1 day
dates.push(date.getMonth()+1 + '/' + date.getDate()); // #1
}
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.
*/
@andreymir
Copy link

For the last question you probably could get 32, 33, 34 days in the end of month :)

@banderson
Copy link
Author

Thanks, good catch! I was too focused on finding the counting bugs caused by zero-based months, etc... and missed the big issue!

Updated the bonus question.

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