Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.29 KB | None | 0 0
  1. - Review: `for` loops
  2.  
  3. + Write a function `foo(n)` such that when you do `foo(3)`, it prints out:
  4.  
  5. 1
  6. 2
  7. 3
  8.  
  9. Each on its own line.
  10.  
  11. + Write a function `bar(n, m)` such that when you do `bar(4, 6)`, it prints out:
  12.  
  13. 4
  14. 5
  15. 6
  16.  
  17. Each on its own line.
  18.  
  19. - Basic loop reading array:
  20. + write a function `printList(arr)` such that `printList([1,2,3])` prints out:
  21.  
  22. 1
  23. 2
  24. 3
  25.  
  26. Each on its own line.
  27. + write a function `sum(arr)` such that `sum([1,2,3])` returns `6`
  28. + write a function `max(arr)` such that `max([1,3,2])` returns `3`
  29.  
  30. - Double-push: write a function `doublePush(arr, x, y)` such that when you do the following:
  31.  
  32. var arr = [1,2,3];
  33. doublePush(arr, "apples", "oranges");
  34. console.log(arr) // => [1, 2, 3, "apples", "oranges"]
  35.  
  36. Then you get the indicated output.
  37.  
  38. - Do the `range()` exercise from Chapter 4 without hints.
  39.  
  40. - Double each: write a function `doubleEach(arr)` such that `doubleEach([1,2,3])` returns `[2,4,6]`
  41.  
  42. - Component-wise addition: write a function `vectorAdd(v, w)` such that:
  43.  
  44. vectorAdd([1,2,3], [1,1,1]) // => [2,3,4]
  45. vectorAdd([1,2,3], [2,0,-1]) // => [3,2,2]
  46. // bonus: trims to shorter of the two arrays
  47. vectorAdd([1,2], [1,2,3]) // => [2,4]
  48. vectorAdd([1,2, 3], [1,2]) // => [2,4]
  49.  
  50. - The `arguments` object:
  51. - write a function that always returns the last argument:
  52.  
  53. last(1,2,3) // => 3
  54. last(1) // => 1
  55. last("apples", "oranges") // => "oranges"
  56. last([1,2,3], "why") // => "why"
  57. last("why", "not", [1,2]) // => [1,2]
  58.  
  59. - write a function that sums its arguments, so `sum(1,2,3)` returns `6`, `sum(4,4,5,5)` returns `18`, etc
  60.  
  61. - somewhat advanced, uses techniques from all of the above: write a function `zip(arr, ...)` that takes any number of arrays, and returns one array of each of their contents zipped together
  62.  
  63. zip([1,3,5], [2,4,6]) // => [[1,2], [3,4], [5,6]]
  64. zip([1,2], [3,4], [5,6]) // => [[1,3,5], [2,4,6]]
  65. // extends to the length of longest array, fill in with undefined
  66. zip([1,2], [1,2,3], [1]) // => [[1,1,1], [2,2,undefined], [undefined,3,undefined]]
  67.  
  68. - array of arrays: write a function `averages(arr, ...)` that takes any number of arrays, and returns an array of their averages
  69.  
  70. averages([1,2,3], [1,1,1], [1,1,2,2]) // => [2, 1, 1.5]
  71.  
  72. - left-pad ragged 2D array of numbers: write a function `leftPadZeroes(arrOfArrays)` that fills in zeroes on the left of the array so that they'll all be the same length (don't know how to prepend something to an array? Google it)
  73.  
  74. leftPadZeroes([[1,2,3], [4], [5,6]]) // => [[1,2,3], [0,0,4], [0,5,6]]
  75. leftPadZeroes([[1,2], [4], [5,6,7,8]]) // => [[0,0,1,2], [0,0,0,4], [5,6,7,8]]
  76.  
  77. - also advanced but similar to `zip()`, matrix multiplication: write a function `matrixMultiply(P, Q)` that takes a _n_×_m_ array of arrays and a _ℓ_×_n_ array of arrays, and matrix-multiplies them to return a _ℓ_×_m_ array of arrays:
  78.  
  79. matrixMultiply(
  80. [[1],
  81. [2],
  82. [3]],
  83. [[1,1,1],
  84. [1,1,1]]
  85. )
  86. // => [[6],[6]]
  87. matrixMultiply(
  88. [[1],
  89. [2],
  90. [3]],
  91. [[1,2,1],
  92. [2,1,2]]
  93. )
  94. // => [[8],[10]]
  95. matrixMultiply(
  96. [[1,2,3]],
  97. [[1],
  98. [1],
  99. [1]]
  100. )
  101. // => [[1,2,3],
  102. // [1,2,3],
  103. // [1,2,3]]
  104. matrixMultiply(
  105. [[1,2],
  106. [3,4]],
  107. [[1,1],
  108. [1,1]]
  109. )
  110. // => [[4,6],
  111. // [4,6]]
  112. matrixMultiply(
  113. [[1,2],
  114. [3,4]],
  115. [[0,1],
  116. [1,0]]
  117. )
  118. // => [[1,2],
  119. // [3,4]]
  120. matrixMultiply(
  121. [[1,2],
  122. [3,4]],
  123. [[1,0],
  124. [0,1]]
  125. )
  126. // => [[3,4],
  127. // [1,2]]
  128.  
  129. ### Objects
  130.  
  131. - Underscore.js
  132. + Write a function `keys(obj)` that retrives an array of the keys (fun fact: you're reimplementing `Object.keys()`. Don't use it):
  133.  
  134. var obj = {
  135. lol: 1,
  136. wut: 2
  137. };
  138. var obj2 = {
  139. haha: 'omg',
  140. 1: 2
  141. };
  142. keys(obj); // => ['lol', 'wut'] (possibly in a different order)
  143. keys(obj2); // => ['haha', '2'] (possibly in a different order)
  144.  
  145. + Write a function `values(obj)` that retrives an array of the values:
  146.  
  147. var obj = {
  148. lol: 1,
  149. wut: 2
  150. };
  151. var obj2 = {
  152. haha: 'omg',
  153. 1: 2
  154. };
  155. keys(obj); // => ['1', '2'] (possibly in a different order)
  156. keys(obj2); // => ['omg', '2'] (possibly in a different order)
  157.  
  158. + (Non-Underscore interstitial) Write a function `sumValues(obj)` that sums the values:
  159.  
  160. var points = {
  161. rick: 101,
  162. morty: 2,
  163. summer: 45,
  164. jerry: -20,
  165. beth: 99.99
  166. };
  167. sumValues(points); // => 227.99
  168.  
  169. + (Non-Underscore interstitial) Write a function `avgValue(obj)` that returns the average of the value:
  170.  
  171. var points = {
  172. team_alpha_ninja: 26,
  173. team_master_blaster: 20,
  174. team_wypipo: 20,
  175. team_untitled2: 18
  176. };
  177. avgValue(points); // => 21
  178.  
  179. + (Non-Underscore interstitial) Write a function `teamNames(obj)` that retrieves an array of keys that start with `'team_'`:
  180.  
  181. var points = {
  182. team_alpha_ninja: 26,
  183. team_master_blaster: 20,
  184. slice: function(){},
  185. team_wypipo: 20,
  186. team_untitled2: 18,
  187. ignore_me: 'lol'
  188. };
  189. teamNames(points); // => ['team_alpha_ninja', 'team_master_blaster', 'team_wypipo', 'team_untitled2'] (possibly in a different order)
  190.  
  191. + Write a function `pairs(obj)` that retrieves key-value pairs:
  192.  
  193. var obj = {
  194. yes: 'no',
  195. omg: 'oh my god',
  196. };
  197. pairs(obj); // => [['yes', 'no'], ['omg', 'oh my god']] (possibly in a different order)
  198.  
  199. + Write a function `invert(obj)` that returns a copy of the object where the keys have become the values and the values the keys:
  200.  
  201. var hotNCold = {
  202. hot: 'cold',
  203. yes: 'no',
  204. wrong: 'right',
  205. black: 'white',
  206. fight: 'break up',
  207. kiss: 'make up',
  208. stay: 'go'
  209. };
  210. invert(hotNCold); /* => {
  211. cold: 'hot',
  212. no: 'yes',
  213. right: 'wrong',
  214. white: 'black',
  215. 'break up': 'fight',
  216. 'make up': 'kiss',
  217. go: 'stay'
  218. }
  219. (possibly in a different order) */
  220.  
  221. // bonus: if any of the values aren't unique, combine into an array
  222. var hotNColder = {
  223. hot: 'cold',
  224. yes: 'no',
  225. wrong: 'right',
  226. black: 'white',
  227. fight: 'break up',
  228. kiss: 'make up',
  229. stay: 'go',
  230. 'ice cream': 'cold',
  231. 'break down': 'break up',
  232. left: 'right',
  233. antarctica: 'cold',
  234. 'ice inside your soul': 'cold'
  235. };
  236. invert(hotNColder); /* => {
  237. cold: ['hot', 'ice cream', 'antarctica', 'ice inside your soul'],
  238. no: 'yes',
  239. right: ['wrong', 'left'],
  240. white: 'black',
  241. 'break up': ['fight', 'break down'],
  242. 'make up': 'kiss',
  243. go: 'stay'
  244. }
  245. (possibly in a different order) */
  246.  
  247. + Write a function `assign(destination, source)` to copy all properties in `source` onto `destination` (fun fact: you're reimplementing `Object.assign()`. Don't use it. Also FYI this is called `_.extend()` on Underscore/Lodash):
  248.  
  249. assign({name: 'Abbi'}, {age: 26}); // => {name: 'moe', age: 50}
  250. // mutates, doesn't copy:
  251. var obj = {name: 'Abbi', age: 26};
  252. assign(obj, {job: 'trainer', city: 'NYC'});
  253. obj // => {name: 'Abbi', age: 26, job: 'trainer', city: 'NYC'}
  254.  
  255. // bonus: multiple source objects
  256. assign({name: 'Abbi'}, {age: 26}, {job: 'trainer', city: 'NYC'}, {bestFriend: 'Ilana'});
  257. // => {name: 'Abbi', age: 26, job: 'trainer', city: 'NYC'}
  258.  
  259. + Write a function `pick(obj, keys)` that returns a copy of `obj`, filtered to only have values for the whitelisted `keys` (an array of strings):
  260.  
  261. pick({name: 'Abbi', age: 26, job: 'trainer', city: 'NYC'}, ['name', 'city']) // => {name: 'Abbi', city: 'NYC'}
  262.  
  263. + Write a function `omit(obj, keys)` that returns a copy of `obj`, filtered to omit the blacklisted `keys` (an array of strings):
  264.  
  265. pick({name: 'Abbi', age: 26, job: 'trainer', city: 'NYC'}, ['age', 'job']) // => {name: 'Abbi', city: 'NYC'}
  266.  
  267. + Write a function `defaults(obj, defaults)` that fills in `undefined` properties in `obj` with the value in `defaults`:
  268.  
  269. defaults({name: 'Abbi', age: 26, city: 'NYC'}, {job: 'unknown', city: 'nowhere'})
  270. // => {name: 'Abbi', age: 26, job: 'unknown', city: 'NYC'}
  271.  
  272. // should mutate the object
  273. var abbi = {name: 'Abbi', age: 26, city: 'NYC'};
  274. defaults(abbi, {job: 'unknown', city: 'nowhere'});
  275. abbi // => {name: 'Abbi', age: 26, job: 'unknown', city: 'NYC'}
  276.  
  277. // bonus: multiple defaults objects
  278. defaults({name: 'Abbi', age: 26, city: 'NYC'}, {job: 'unknown', city: 'nowhere'}, {website: 'example.com'})
  279. // => {name: 'Abbi', age: 26, job: 'unknown', city: 'NYC', website: 'example.com'}
  280.  
  281. + Write a function `isMatch(obj, props)` that tells you if the keys and values in `props` are contained in `obj`:
  282.  
  283. isMatch({name: 'Abbi', age: 26, city: 'NYC', job: 'trainer'}, {name: 'Abbi'}) // => true
  284. isMatch({name: 'Abbi', age: 26, city: 'NYC', job: 'trainer'}, {name: 'Ilana'}) // => false
  285. isMatch({name: 'Abbi', age: 26, city: 'NYC', job: 'trainer'}, {}) // => true
  286. isMatch({name: 'Abbi', age: 26, city: 'NYC', job: 'trainer'}, {bestFriend: 'Ilana'}) // => false
  287. isMatch({name: 'Abbi', age: 26, city: 'NYC', job: 'trainer'}, {city: 'NYC', bestFriend: 'Ilana'}) // => false
  288. isMatch({name: 'Abbi', age: 26, city: 'NYC', job: 'trainer'}, {city: 'NYC', age: 26}) // => true
  289. isMatch({name: 'Abbi', age: 26, city: 'NYC', job: 'trainer'}, {city: 'NYC', age: 27}) // => false
  290.  
  291. + Write a function `isEmpty(obj)` that tells you if the object has no keys:
  292.  
  293. isEmpty({}) // => true
  294. isEmpty({yep: 'nope'}) // => false
  295.  
  296. + Write a function `pluck(objs, key)` that takes an array of objects and property name and extracts an array of property values:
  297.  
  298. var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
  299. pluck(stooges, 'name') // => ["moe", "larry", "curly"]
  300.  
  301. - Basic object key string access: write a function `sumOfTwoKeys(obj, key1, key2, newKey)` such that
  302.  
  303. var obj = {
  304. lol: 1,
  305. wut: 100,
  306. irrelevant: 2
  307. }
  308. sumOfTwoKeys(obj, "lol", "wut", "lolwut")
  309. obj // => { lol: 1, wut: 100, irrelevant: 2, lolwut: 101 }
  310.  
  311. var obj2 = {
  312. lol: 1,
  313. wut: 2,
  314. thing: 10
  315. }
  316. sumOfTwoKeys(obj, "lol", "wut", "thing")
  317. obj2 // => { lol: 1, wut: 2, thing: 3 }
  318.  
  319. - Histogram (okay, now we're veering into the territory of realistic, useful code/realistic easy interview questions!): write a function `histogram(arrayOfStrings)` such that
  320.  
  321. histogram(["aardvark", "words", "best", "words"])
  322. // => { aardvark: 1, best: 1, words: 2 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement