Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 1: how could you rewrite the following to make it shorter?
// Naming methods with superfluous characters that are not descriptive is a waste of time, so I would shorten the method names.
if (foo) {
bar.something(el);
} else {
bar.somethingElse(el);
}
// 2: what is the faulty logic in the following code?
// The variable foo is scoped to the self-executing anonymous function.
// Using a global variable is recommended if you need to share variable scope
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();
//To affect the foo without affecting the bar, set it only on foo
foo.bar = "bam";
//To affect both of them, set it on both of them.
var bam = "bam";
foo.bar = bam;
bim.bar = bam;
// 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()
};
myObjects.thinger.destroy();
myObjects.gizmo.destroy()
myObjects.widget.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 jQuery, dojo, or underscore is
// available)
var myArray = [ 'foo', 'bar', 'baz' ];
var myArray2 = [ 'foo foo foo', 'bar bar bar', 'baz baz baz'];
// 6: what potential issues do you see with the following code? how would you fix it?
// I heard in a few places that you can't be 100% sure document ready really works, so you should use window.load to be safe
$(window).load(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();
});
// 7: what issues do you see with the following code? how would you fix it?
// Uhh... it uses dojo?? Jquery IS THE BEST!!!!!!!!111
(function() {
var foo;
dojo.xhrGet({
url : 'foo.php',
load : function(resp) {
foo = resp.foo;
}
});
if (foo) {
// run this important code
}
})();
// 8: how could you rewrite the following code to make it shorter?
// You can eliminate the duplicate code by giving the same class to the elements and then selecting them all
$('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');
// --->
$("li.bar,li.baz,li.bop").addClass("foo");
$("li.foo a").each(function() {
$(this).attr("title","I am " + $(this).attr("class"));
});
// 9: how would you improve the following code?
// "thinger" and "gizmo" are not valid metasyntactic variables, please use 'foo' and 'bar'
for (i = 0; i <= 100; i++) {
$('#foo').append('<p><span class="thinger">i am thinger ' + i + '</span></p>');
$('#bar').append('<p><span class="gizmo">i am gizmo ' + i + '</span></p>');
}
// 10: a user enters their desired tip into a text box; the baseTotal, tax,
// and fee values are provided by the application. what are the potential
// issues with the following function for calculating the total?
//
// You have to force the values to be numbers, even if they aren't.
function calculateTotal(baseTotal, tip, tax, fee) {
return Number(baseTotal) + Number(tip) + Number(tax) + Number(fee);
}
// BONUS: what is the faulty logic in the following code? how would you fix it?
//
// Dates are supposed to have the date first and the month second, like in europe, ok?
var date = new Date(),
day = date.getDate(),
month = date.getMonth(),
dates = [];
for (var i = 0; i <= 5; i++) {
dates.push(day+i + '/' + month))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment