Guest User

Untitled

a guest
Jun 14th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. // 1: how could you rewrite the following to make it shorter?
  2. // Naming methods with superfluous characters that are not descriptive is a waste of time, so I would shorten the method names.
  3. if (foo) {
  4. bar.something(el);
  5. } else {
  6. bar.somethingElse(el);
  7. }
  8.  
  9.  
  10.  
  11.  
  12. // 2: what is the faulty logic in the following code?
  13. // The variable foo is scoped to the self-executing anonymous function.
  14. // Using a global variable is recommended if you need to share variable scope
  15. foo = 'hello';
  16.  
  17. (function() {
  18. foo = foo || 'world';
  19. console.log(foo);
  20. })();
  21.  
  22.  
  23.  
  24.  
  25. // 3: given the following code, how would you override the value of the bar
  26. // property for the variable foo without affecting the value of the bar
  27. // property for the variable bim? how would you affect the value of the bar
  28. // property for both foo and bim? how would you add a method to foo and bim to
  29. // console.log the value of each object's bar property? how would you tell if
  30. // the object's bar property had been overridden for the particular object?
  31. var Thinger = function() {
  32. return this;
  33. };
  34.  
  35. Thinger.prototype = {
  36. bar : 'baz'
  37. };
  38.  
  39. var foo = new Thinger(),
  40. bim = new Thinger();
  41.  
  42.  
  43. //To affect the foo without affecting the bar, set it only on foo
  44. foo.bar = "bam";
  45.  
  46. //To affect both of them, set it on both of them.
  47. var bam = "bam";
  48. foo.bar = bam;
  49. bim.bar = bam;
  50.  
  51.  
  52.  
  53.  
  54. // 4: given the following code, and assuming that each defined object has a
  55. // 'destroy' method, how would you destroy all of the objects contained in the
  56. // myObjects object?
  57. var myObjects = {
  58. thinger : new myApp.Thinger(),
  59. gizmo : new myApp.Gizmo(),
  60. widget : new myApp.Widget()
  61. };
  62.  
  63.  
  64. myObjects.thinger.destroy();
  65. myObjects.gizmo.destroy()
  66. myObjects.widget.destroy();
  67.  
  68.  
  69. // 5: given the following array, create an array that contains the contents of
  70. // each array item repeated three times, with a space between each item. so,
  71. // for example, if an array item is 'foo' then the new array should contain an
  72. // array item 'foo foo foo'. (you can assume jQuery, dojo, or underscore is
  73. // available)
  74. var myArray = [ 'foo', 'bar', 'baz' ];
  75.  
  76. var myArray2 = [ 'foo foo foo', 'bar bar bar', 'baz baz baz'];
  77.  
  78.  
  79. // 6: what potential issues do you see with the following code? how would you fix it?
  80. // 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
  81. $(window).load(function() {
  82. $('.foo #bar').css('color', 'red');
  83. $('.foo #bar').css('border', '1px solid blue');
  84. $('.foo #bar').text('new text!');
  85. $('.foo #bar').click(function() {
  86. $(this).attr('title', 'new title');
  87. $(this).width('100px');
  88. });
  89.  
  90. $('.foo #bar').click();
  91. });
  92.  
  93.  
  94.  
  95.  
  96. // 7: what issues do you see with the following code? how would you fix it?
  97. // Uhh... it uses dojo?? Jquery IS THE BEST!!!!!!!!111
  98. (function() {
  99. var foo;
  100.  
  101. dojo.xhrGet({
  102. url : 'foo.php',
  103. load : function(resp) {
  104. foo = resp.foo;
  105. }
  106. });
  107.  
  108. if (foo) {
  109. // run this important code
  110. }
  111. })();
  112.  
  113.  
  114.  
  115.  
  116. // 8: how could you rewrite the following code to make it shorter?
  117. // You can eliminate the duplicate code by giving the same class to the elements and then selecting them all
  118. $('li.foo a').attr('title', 'i am foo');
  119. $('li.bar a').attr('title', 'i am bar');
  120. $('li.baz a').attr('title', 'i am baz');
  121. $('li.bop a').attr('title', 'i am bop');
  122. // --->
  123. $("li.bar,li.baz,li.bop").addClass("foo");
  124. $("li.foo a").each(function() {
  125. $(this).attr("title","I am " + $(this).attr("class"));
  126. });
  127.  
  128.  
  129.  
  130.  
  131. // 9: how would you improve the following code?
  132. // "thinger" and "gizmo" are not valid metasyntactic variables, please use 'foo' and 'bar'
  133. for (i = 0; i <= 100; i++) {
  134. $('#foo').append('<p><span class="thinger">i am thinger ' + i + '</span></p>');
  135. $('#bar').append('<p><span class="gizmo">i am gizmo ' + i + '</span></p>');
  136. }
  137.  
  138.  
  139.  
  140.  
  141. // 10: a user enters their desired tip into a text box; the baseTotal, tax,
  142. // and fee values are provided by the application. what are the potential
  143. // issues with the following function for calculating the total?
  144. //
  145. // You have to force the values to be numbers, even if they aren't.
  146. function calculateTotal(baseTotal, tip, tax, fee) {
  147. return Number(baseTotal) + Number(tip) + Number(tax) + Number(fee);
  148. }
  149.  
  150.  
  151.  
  152.  
  153. // BONUS: what is the faulty logic in the following code? how would you fix it?
  154. //
  155. // Dates are supposed to have the date first and the month second, like in europe, ok?
  156. var date = new Date(),
  157. day = date.getDate(),
  158. month = date.getMonth(),
  159. dates = [];
  160.  
  161. for (var i = 0; i <= 5; i++) {
  162. dates.push(day+i + '/' + month))
  163. }
Add Comment
Please, Sign In to add comment