Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1: how could you rewrite the following to make it shorter?
- if (foo) {
- bar.doSomething(el);
- } else {
- bar.doSomethingElse(el);
- }
- bar[foo ? 'doSomething' : 'doSomethingElse'](el); // stolen from somewhere.
- // 2: what is the faulty logic in the following code?
- var foo = 'hello';
- (function() {
- var foo = foo || 'world';
- console.log(foo);
- })();
- // Answer: foo will be undefined within the closure. console will always output 'world'.
- // 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();
- // A:
- foo.bar = 'new value';
- // B:
- Thinger.prototype.bar = 'newer value';
- // C:
- Thinger.prototype.debug = function () {
- console.log(this.bar);
- }
- // D: ?
- Thinger.prototype.overridden = function () {
- if (this.hasOwnProperty("bar")) {
- console.log("this has not been overridden")
- }
- else {
- console.log("this has been overridden");
- }
- }
- // 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 (obj in myObjects) {
- 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' ];
- var newArray = [];
- for (var i = myArray.length; i--;) {
- newArray[i] = myArray[i].replace(/.*/, "$& $& $&");
- }
- newArray;
- // 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();
- });
- // First try. Still thinking there's a better way. I know we can pass a JSON object to CSS. Maybe that is a better way?
- $(document).ready(function() {
- $('#bar')
- .css('color', 'red')
- .css('border', '1px solid blue')
- .text('new text!')
- .attr('title', 'new title')
- .width('100px');
- .click(function() {
- $(this)
- .attr('title', 'new title')
- .width('100px');
- });
- });
- // 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
- }
- })();
- // xhrGet is asynchronous
- (function() {
- dojo.xhrGet({
- url : 'foo.php',
- load : function(resp) {
- if (resp.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);
- // I'm not familiar with dojo, but here's my first shot...
- (function(d, $){
- $('li a').each(function () {
- if (['foo', 'bar', 'baz', 'bop'].contains($(this).getClass()) {
- $(this).attr('title', 'i am ' $(this).getClass());
- }
- });
- })(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>');
- }
- // Not prettier...
- (function () {
- var thingerText = [],
- gizmoText = [];
- function createPAndSpan(text, i) {
- var text = [];
- text.push('<p><span class="');
- text.push(text);
- text.push('">i am ');
- text.push(text);
- text.push(' ');
- text.push(i);
- text.push('</span></p>');
- return text.join('');
- }
- for (i = 0; i <= 100; i++) {
- thingerText.push(createPAndSpan('thinger', i));
- gizmoText.push(createPAndSpan('gizmo', i));
- }
- $('#thinger').append(thingerText.join(''));
- $('#gizmo').append(gizmoText.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;
- }
- // if 'tip' is a String, it could be concatenated as such:
- calculateTotal(1, "0", 1, 1); // will output "1011"
- // tip might be negative
- // 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 newArray = [];
- for(var i = 0, length = menuItems.length; i < length; i++) {
- newArray.push(menuItems[i].name + ("extras" in menuItems[i] ? " (" + menuItems[i].extras.join(", ") + ")" : ""));
- };
- // BONUS: what is the faulty logic in the following code? how would you fix it?
- var date = new Date(2010, 8, 30),
- 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(', '));
- // still working on this too, but I found the faulty logic by setting the date to 2010-08-30
- /*
- 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.
- */
Add Comment
Please, Sign In to add comment