elyseholladay
12/17/2012 - 7:51 PM

screening.js

// 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?
var foo = 'hello';

(function() {
    var foo = window.foo || 'world';
    console.log(foo);
})();


// 3: given the following code, 
// a) 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? 
// b) how would you affect the value of the bar property for both foo and bim? 
// c) how would you add a method to foo and bim to console.log the value of each object's bar property? 
// d) 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();

// 3a
foo.bar = "baz1";
// 3b 
Thinger.prototype.bar = "sdfsdafs";

// 3c
Thinger.prototype.log = function() {
    console.log(this.bar)
};

foo.log();

// 3d 
bim.bar == bim.__proto__.bar

// 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 key in myObjects) {
    myObjects.hasOwnProperty(key) && myObjects[key].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'];

for (var i = myArray.length - 1; i >= 0; i--) {
    var val = myArray[i];
    var tmp = [val, val, val];
    myArray[i] = tmp.join(" ");
}
console.log(myArray);



// 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();
});


$(function() {
    $('.foo #bar').css({
        'color': 'red',
        'border': '1px solid blue'
    }).text('new text!').click(function() {
        $(this).attr('title', 'new title');
        $(this).width('100px');
    }).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
    }
})();



(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);

(function(d, $) {
    function setTitle() {
        $('li.' + name + ' a').attr('title', 'i am ' + name);
    }
    setTitle('foo');
    setTitle('bar');
    setTitle('baz');
    setTitle('bop');
})(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>');
}

var appThinger = [];
var appGizmo = [];

for (i = 0; i <= 100; i++) {
    appThinger.push('<p><span class="thinger">i am thinger ' + i + '</span></p>');
    appGizmo.push('<p><span class="gizmo">i am gizmo ' + i + '</span></p>');
}

$('#thinger').append(appThinger.join());
$('#gizmo').append(appGizmo.join());





// 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;
}

function calculateTotal(baseTotal, tip, tax, fee) {
    baseTotal = baseTotal || 0;
    tip = tip || 0; // usr input int/float checks etc...
    tax = tax || 0;
    fee = fee || 0;

    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'}];

var items = [];
for (var k in menuItems) {
    if (menuItems.hasOwnProperty(k)) {
        var item = menuItems[k];
        items.push(item.name + (item.extras ? (' (' + item.extras.join(", ") + ')') : ''));
    }
}
console.log(items);


// BONUS: write code such that the following alerts "Hello World"
say('Hello')('World');




// 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(', '));



/*
          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.
*/​