public
Created — forked from rmurphey/screening.js

  • Download Gist
screening.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
// 1: how could you rewrite the following to make it shorter?
if (foo) {
bar.doSomething(el);
} else {
bar.doSomethingElse(el);
}
 
 
 
 
// 2: what is the faulty logic in the following code?
var foo = 'hello';
 
(function() {
var 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();
 
 
 
 
// 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()
};
 
 
 
 
// 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' ];
 
 
 
 
// 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();
});
 
 
 
 
// 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
}
})();
 
 
 
 
// 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);
 
 
 
 
// 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>');
}
 
 
 
 
// 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;
}
 
 
 
 
// 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'
}
];
 
 
 
 
// 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(', '));

#1
foo ? bar.doSomething(el) : bar.doSomethingElse(el);

#2
first foo is not accessible inside the closure, so there will always log "world"

#3
1. foo.bar = "test"
2. Thinger.bar = "test"
3. Thinger.prototype.log = function(){console.log(this.bar)}
foo.log();
bim.log();
4. foo.bar = 2
alert(Object.getPrototypeOf(foo).bar === foo.bar);

#4
for(var i in myObjects){
myObjects[i].destroy();
delete myObjects[i];
}

#5
$(myArray).each(function(i){
var item = myArray[i];
item = new Array(item+" "+item+" "+item);
alert(item)
});

#6
$(document).ready(function() {
$('.foo #bar')
.css({
'color': 'red',
'border': '1px solid blue'
})
.text('new text!')
.bind('click', function(){
$(this)
.attr('title', 'new title')
.width('100px');
})
});

#7
dojo.xhrGet is asynchronous, so foo will always be undefined.
You can set sync: true in your xhrGet or move if(foo){...} inside the load callback function

#8
(function(d, $){
var listItems = $('li');
d.forEach($('li'),function(i){
d.attr(i.firstChild, "title", "i am "+i.className);
});
})(dojo, dojo.query);

#9

var populate = function($targets,text){
$($targets).each(function(){
this.append(text.replace(/\$id/g, this.attr("id")));
});
}
var t = "";

for (var i = 0; i <= 10; i++) {
t += '

i am $id ' + i + '

';
}
populate([$("#thinger"),$("#gizmo")], t);

#10
You can't assume that tip is a number so you better write to ignore tip if NaN
function calculateTotal(baseTotal, tip, tax, fee) {
return baseTotal + (parseInt(tip,10) || 0) + tax + fee;
}

#11
var a = [];
$(menuItems).each(function(){
a.push(this.extras && this.extras.length ? this.name +" ("+this.extras.join(', ')+")" : this.name);
});

#BONUS
alert('The next five days are '+ dates.join(', '));

:D Nice thank you

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.