SHARE
TWEET

Untitled

a guest May 15th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Function declaration & Function expression
  2. ## Q1
  3. ```js
  4. console.log(square(5));
  5. function square(n) {
  6. return n*n;
  7. }
  8. ```
  9. - Error
  10. - 25
  11. - 5
  12. - 0
  13. ## Q2
  14. ```js
  15. console.log(square(5));
  16. var square = function(n) {
  17. return n*n;
  18. }
  19. ```
  20. - 25
  21. - Error
  22. - 5
  23. - 0
  24. ## Q3
  25. ```js
  26. var square = (n) => {
  27. return n*n;
  28. }
  29. console.log(square(3));
  30. ```
  31. - 25
  32. - 9
  33. - Error
  34. - 5
  35. ## Q4
  36. ```js
  37. let square = 3
  38. console.log(square(5))
  39. function square(n) {
  40. return n
  41. }
  42. ```
  43. - Error
  44. - 9
  45. - 0
  46. - 25
  47. ## Q5
  48. ```js
  49. let square = 3
  50. function square(n) {
  51. return n
  52. }
  53. console.log(square(5))
  54. ```
  55. - Error
  56. - 5
  57. - 0
  58. - 25
  59.  
  60. # Recursive functions
  61. ## Q1 & Q2
  62. ```js
  63.   function recurse_1(param) {
  64.     if (param) {
  65.       return "done";
  66.     } else {
  67.       const not_param = !param
  68.       return recurse_1(not_param);
  69.     }
  70.   }
  71. ```
  72.   // Q1: what does this return ?
  73.   const ret_val = recurse_1(true);
  74.  
  75.   // Q2: what does this return ?
  76.   const ret_val = recurse_1(false);
  77.  
  78. ## Q3 & Q4
  79. ```js
  80.   function recurse_2(param) {
  81.     if (param) {
  82.       const not_param = !param
  83.       return recurse_2(not_param);
  84.     } else {
  85.       return "done";
  86.     }
  87.   }
  88. ```
  89.   // Q1: what does this return ?
  90.   const ret_val = recurse_2(true);
  91.   // Q2: what does this return ?
  92.   const ret_val = recurse_2(false);
  93.  
  94.  
  95. # array.map()
  96. ## Q1
  97. ```js
  98. function divideByTwo(num) {
  99.   return num/2
  100. }
  101. let array = [12, 4, 2]
  102. let result = array.map(divideByTwo)
  103. console.log(result)
  104. ```
  105. - 3
  106. - [12, 4, 2]
  107. - [6, 2, 1]
  108.  
  109. ## Q2
  110. ```js
  111. let books = ['Harry Potter', 'One Piece', 'Midnight Murder']
  112. function getLength (str='') {
  113.  return str.length
  114. }
  115. let result = books.map(getLength)
  116. console.log(result)
  117. ```
  118. - ['', '', '']
  119. - [12, 9, 15]
  120. - 3
  121. - Error
  122.  
  123. ## Q3
  124. ```js
  125. let fibonacci = [0, 1, 1, 2, 3, 5, 8]
  126. let result = fibonacci.map((val) => val*-1)
  127. console.log(result)
  128. ```
  129. - [0, 1, 1, 2, 3, 5, 8]
  130. - [-1, 0, 0, 1, 2, 4, 7]
  131. - [0, -1, -1, -2, -3, -5, -8]
  132.  
  133. ## Q4
  134. ```js
  135. let result = [true, false, true, true].map(b => !b)
  136. console.log(result)
  137. ```
  138. - Error
  139. - [false, true, false, false]
  140. - [true, false, true, true]
  141. - [true, true, true, true]
  142.  
  143. ## Q5
  144. ```js
  145. let result = ['papa', 'mama', 'cat', 'dog'].map(val => 1)
  146. console.log(result)
  147. ```
  148. - Error
  149. - [1, 1, 1, 1]
  150. - ['papa', 'mama', 'cat', 'dog']
  151.  
  152. # array.filter()
  153. ## Q1
  154. ```js
  155. const fruits = ['apple', 'banana', 'grapes', 'mango', 'orange']
  156. function getFruitILike(fruit) {
  157.   return fruit.indexOf('n') === -1
  158. }
  159. let result = fruits.filter(getFruitILike)
  160. ```
  161. ## Q2
  162. ```js
  163. const countries = ['EN', 'BE', 'NL', 'JA', 'GA', 'USA']
  164. function keepLongCountryCode(code) {
  165.   return code.length > 2
  166. }
  167. let result = countries.filter(keepLongCountryCode)
  168. console.log(result)
  169. ```
  170. - ['EN', 'BE', 'NL', 'JA', 'GA', 'USA']
  171. - ['EN', 'BE', 'NL', 'JA', 'GA']
  172. - ['USA']
  173. - 'USA'
  174.  
  175. ## Q3
  176. ```js
  177. let lang = ['golang', 'python', 'C++', 'javascript', 'C', 'ruby']
  178. let result = lang.filter(lang => lang[0] === 'C')
  179. console.log(result.length)
  180. ```
  181.  
  182. - ['C++', 'C']
  183. - 2
  184. - ['golang', 'python', 'javascript', 'ruby']
  185.  
  186. ```
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top