Skip to content

Instantly share code, notes, and snippets.

@casecode
Forked from tomfuertes/screening.js
Last active August 29, 2015 14:11
Show Gist options
  • Save casecode/1b4da47cbd93b40e2047 to your computer and use it in GitHub Desktop.
Save casecode/1b4da47cbd93b40e2047 to your computer and use it in GitHub Desktop.
/*
screening.js test forked from rmurphy via tomfuertes
Answers by casecode
*/
// 1: how could you rewrite the following to make it shorter?
if (foo) {
bar.doSomething(el);
} else {
bar.doSomethingElse(el);
}
// ANSWER
// Option 1
foo? bar.doSomething(el) : bar.doSomethingElse(el);
// Option 2
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);
})();
// ANSWER
// foo is not defined in the scope of the immediately-invoked function expression
var foo = 'hello';
(function() {
var foo = window.foo || 'world'; // foo declared in global scope on window
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();
// ANSWER
// override foo.bar without affecting bim.bar
foo.bar = 'biz';
// affect the value of the bar property for both foo and bim
Thinger.prototype.bar = 'biz';
// add a method to foo and bim to console.log the value of each object's bar property
Thinger.prototype.logBar = function() { console.log(this.bar); };
// determine if the object's bar property had been overridden for the particular object
// Option 1 - determine if foo has its own bar property
foo.hasOwnProperty('bar');
// Option 2 - determine if value has actually changed
Thinger.prototype.observedPropertyOverwrite = function(property) {
if (this[property] === Thinger.prototype[property]) return false;
return true;
};
foo.observedPropertyOverwrite('bar');
// Option 3 - Using KVO
function barChanged(newValue) {
console.log("Value of bar changed to: " + newValue);
}
foo.__defineSetter__('bar', barChanged);
// 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
Object.keys(myObjects).forEach(function(k) {
myObjects[k].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
// return new array
Array.prototype.tripleTheFun = function() {
return this.map(function(el) {
return [el, el, el].join(' ');
});
};
var tripleFunArray = myArray.tripleTheFun();
// update in place
Array.prototype.tripleTheFunInPlace = function() {
var _this = this;
_this.forEach(function(el, i) {
_this[i] = [el, el, el].join(' ');
})
};
myArray.tripleTheFunInPlace();
// 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
$(document).ready(function() {
var $bar = $('.foo #bar');
$bar.css({
'color': 'red',
'border': '1px solid blue'
});
$bar.text('new text!');
function onBarClick() {
$bar.attr('title', 'new title');
$bar.width('100px');
};
$bar.click(onBarClick);
});
// 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
// Race condition - load callback is asynchronous, so if block may execute prior to receiving response
(function() {
dojo.xhrGet({
url : 'foo.php',
load : function(resp) {
var foo = resp.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
'foo bar baz bop'.split(' ').forEach(function (sel) {
dojo.query('li.' + sel + ' a').attr('title', 'i am ' + sel);
});
// 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 creatures = ['thinger', 'gizmo'];
creatures.forEach(function(creature) {
var html = [];
for (var i = 0; i <= 100; i++) {
html.push('<p><span class="' + creature + '">i am ' + creature + ' ' + i + '</span></p>');
}
$('#' + creature).append(html);
})
// 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
// Tip may not be a number type
function validateTip(tip) {
var valid = false;
while (!valid) {
tip = parseFloat(tip, 10);
if (isNaN(tip)) {
var tip = window.prompt("Please enter a valid tip");
continue;
}
valid = true;
}
return tip;
}
function calculateTotal(baseTotal, tip, tax, fee) {
tip = validateTip(tip);
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'
}
];
// ANSWER
// Option 1
var menu = menuItems.map(function(item) {
if (!item.extras) return item.name;
return item.name + ' (' + item.extras.join(', ') + ')';
});
// Option 2 - call anonymous function
var menu = [];
for (var i = 0; i < menuItems.length; i++) {
(function() {
var item = this.name + (this.extras? ( ' (' + this.extras.join(', ') + ')' ) : '');
menu.push(item);
}).call(menuItems[i]);
}
// BONUS: write code such that the following alerts "Hello World"
say('Hello')('World');
// ANSWER
function say(str1) {
return function(str2) {
alert(str1 + ' ' + str2);
};
}
// 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
// Index should start at 1 (tomorrow)
// Adding i to date could produce days beyond 31
// getMonth counts from 0 - 11, need to increment by 1
Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
};
var today = new Date(),
dates = [];
for (var i = 1; i <= 5; i++) {
var d = today.addDays(i);
dates.push((d.getMonth() + 1) + '/' + d.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