Guest User

Untitled

a guest
Jun 14th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.00 KB | None | 0 0
  1. // 1: how could you rewrite the following to make it shorter?
  2. if (foo) {
  3. bar.doSomething(el);
  4. } else {
  5. bar.doSomethingElse(el);
  6. }
  7.  
  8. bar[foo ? 'doSomething' : 'doSomethingElse'](el); // stolen from somewhere.
  9.  
  10.  
  11. // 2: what is the faulty logic in the following code?
  12. var foo = 'hello';
  13.  
  14. (function() {
  15. var foo = foo || 'world';
  16. console.log(foo);
  17. })();
  18.  
  19. // Answer: foo will be undefined within the closure. console will always output 'world'.
  20.  
  21.  
  22.  
  23. // 3: given the following code, how would you override the value of the bar
  24. // property for the variable foo without affecting the value of the bar
  25. // property for the variable bim? how would you affect the value of the bar
  26. // property for both foo and bim? how would you add a method to foo and bim to
  27. // console.log the value of each object's bar property? how would you tell if
  28. // the object's bar property had been overridden for the particular object?
  29. var Thinger = function() {
  30. return this;
  31. };
  32.  
  33. Thinger.prototype = {
  34. bar : 'baz'
  35. };
  36.  
  37. var foo = new Thinger(),
  38. bim = new Thinger();
  39.  
  40. // A:
  41. foo.bar = 'new value';
  42.  
  43. // B:
  44. Thinger.prototype.bar = 'newer value';
  45.  
  46. // C:
  47. Thinger.prototype.debug = function () {
  48. console.log(this.bar);
  49. }
  50.  
  51. // D: ?
  52. Thinger.prototype.overridden = function () {
  53. if (this.hasOwnProperty("bar")) {
  54. console.log("this has not been overridden")
  55. }
  56. else {
  57. console.log("this has been overridden");
  58. }
  59. }
  60.  
  61.  
  62. // 4: given the following code, and assuming that each defined object has a
  63. // 'destroy' method, how would you destroy all of the objects contained in the
  64. // myObjects object?
  65. var myObjects = {
  66. thinger : new myApp.Thinger(),
  67. gizmo : new myApp.Gizmo(),
  68. widget : new myApp.Widget()
  69. };
  70.  
  71. for (obj in myObjects) {
  72. obj.destroy();
  73. }
  74.  
  75.  
  76. // 5: given the following array, create an array that contains the contents of
  77. // each array item repeated three times, with a space between each item. so,
  78. // for example, if an array item is 'foo' then the new array should contain an
  79. // array item 'foo foo foo'. (you can assume the library of your choice is
  80. // available)
  81. var myArray = [ 'foo', 'bar', 'baz' ];
  82.  
  83. var newArray = [];
  84. for (var i = myArray.length; i--;) {
  85. newArray[i] = myArray[i].replace(/.*/, "$& $& $&");
  86. }
  87. newArray;
  88.  
  89.  
  90. // 6: how could you improve the following code?
  91. $(document).ready(function() {
  92. $('.foo #bar').css('color', 'red');
  93. $('.foo #bar').css('border', '1px solid blue');
  94. $('.foo #bar').text('new text!');
  95. $('.foo #bar').click(function() {
  96. $(this).attr('title', 'new title');
  97. $(this).width('100px');
  98. });
  99.  
  100. $('.foo #bar').click();
  101. });
  102.  
  103.  
  104. // 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?
  105. $(document).ready(function() {
  106. $('#bar')
  107. .css('color', 'red')
  108. .css('border', '1px solid blue')
  109. .text('new text!')
  110. .attr('title', 'new title')
  111. .width('100px');
  112. .click(function() {
  113. $(this)
  114. .attr('title', 'new title')
  115. .width('100px');
  116. });
  117. });
  118.  
  119.  
  120.  
  121.  
  122. // 7: what issues do you see with the following code? how would you fix it?
  123. (function() {
  124. var foo;
  125.  
  126. dojo.xhrGet({
  127. url : 'foo.php',
  128. load : function(resp) {
  129. foo = resp.foo;
  130. }
  131. });
  132.  
  133. if (foo) {
  134. // run this important code
  135. }
  136. })();
  137.  
  138. // xhrGet is asynchronous
  139. (function() {
  140. dojo.xhrGet({
  141. url : 'foo.php',
  142. load : function(resp) {
  143. if (resp.foo) {
  144. // run this important code
  145. }
  146. }
  147. });
  148. })();
  149.  
  150.  
  151.  
  152. // 8: how could you rewrite the following code to make it shorter?
  153. (function(d, $){
  154. $('li.foo a').attr('title', 'i am foo');
  155. $('li.bar a').attr('title', 'i am bar');
  156. $('li.baz a').attr('title', 'i am baz');
  157. $('li.bop a').attr('title', 'i am bop');
  158. })(dojo, dojo.query);
  159.  
  160. // I'm not familiar with dojo, but here's my first shot...
  161. (function(d, $){
  162. $('li a').each(function () {
  163. if (['foo', 'bar', 'baz', 'bop'].contains($(this).getClass()) {
  164. $(this).attr('title', 'i am ' $(this).getClass());
  165. }
  166. });
  167. })(dojo, dojo.query);
  168.  
  169.  
  170. // 9: how would you improve the following code?
  171. for (i = 0; i <= 100; i++) {
  172. $('#thinger').append('<p><span class="thinger">i am thinger ' + i + '</span></p>');
  173. $('#gizmo').append('<p><span class="gizmo">i am gizmo ' + i + '</span></p>');
  174. }
  175.  
  176.  
  177. // Not prettier...
  178. (function () {
  179. var thingerText = [],
  180. gizmoText = [];
  181.  
  182. function createPAndSpan(text, i) {
  183. var text = [];
  184. text.push('<p><span class="');
  185. text.push(text);
  186. text.push('">i am ');
  187. text.push(text);
  188. text.push(' ');
  189. text.push(i);
  190. text.push('</span></p>');
  191. return text.join('');
  192. }
  193.  
  194. for (i = 0; i <= 100; i++) {
  195. thingerText.push(createPAndSpan('thinger', i));
  196. gizmoText.push(createPAndSpan('gizmo', i));
  197. }
  198.  
  199. $('#thinger').append(thingerText.join(''));
  200. $('#gizmo').append(gizmoText.join(''));
  201. })();
  202.  
  203.  
  204. // 10: a user enters their desired tip into a text box; the baseTotal, tax,
  205. // and fee values are provided by the application. what are some potential
  206. // issues with the following function for calculating the total?
  207. function calculateTotal(baseTotal, tip, tax, fee) {
  208. return baseTotal + tip + tax + fee;
  209. }
  210.  
  211. // if 'tip' is a String, it could be concatenated as such:
  212. calculateTotal(1, "0", 1, 1); // will output "1011"
  213. // tip might be negative
  214.  
  215.  
  216. // 11: given the following data structure, write code that returns an array
  217. // containing the name of each item, followed by a comma-separated list of
  218. // the item's extras, if it has any. e.g.
  219. //
  220. // [ "Salad (Chicken, Steak, Shrimp)", ... ]
  221. //
  222. // (you can assume the library of your choice is available)
  223. var menuItems = [
  224. {
  225. id : 1,
  226. name : 'Salad',
  227. extras : [
  228. 'Chicken', 'Steak', 'Shrimp'
  229. ]
  230. },
  231.  
  232. {
  233. id : 2,
  234. name : 'Potato',
  235. extras : [
  236. 'Bacon', 'Sour Cream', 'Shrimp'
  237. ]
  238. },
  239.  
  240. {
  241. id : 3,
  242. name : 'Sandwich',
  243. extras : [
  244. 'Turkey', 'Bacon'
  245. ]
  246. },
  247.  
  248. {
  249. id : 4,
  250. name : 'Bread'
  251. }
  252. ];
  253.  
  254. var newArray = [];
  255. for(var i = 0, length = menuItems.length; i < length; i++) {
  256. newArray.push(menuItems[i].name + ("extras" in menuItems[i] ? " (" + menuItems[i].extras.join(", ") + ")" : ""));
  257. };
  258.  
  259.  
  260. // BONUS: what is the faulty logic in the following code? how would you fix it?
  261. var date = new Date(2010, 8, 30),
  262. day = date.getDate(),
  263. month = date.getMonth(),
  264. dates = [];
  265.  
  266. for (var i = 0; i <= 5; i++) {
  267. dates.push(month + '/' + (day + i));
  268. }
  269.  
  270. console.log('The next five days are ', dates.join(', '));
  271.  
  272. // still working on this too, but I found the faulty logic by setting the date to 2010-08-30
  273.  
  274. /*
  275. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  276. Version 2, December 2004
  277.  
  278. Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
  279.  
  280. Everyone is permitted to copy and distribute verbatim or modified
  281. copies of this license document, and changing it is allowed as long
  282. as the name is changed.
  283.  
  284. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  285. TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  286.  
  287. 0. You just DO WHAT THE FUCK YOU WANT TO.
  288. */
Add Comment
Please, Sign In to add comment