Advertisement
Guest User

Untitled

a guest
Apr 17th, 2018
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.68 KB | None | 0 0
  1. /*
  2. Once you complete a problem, refresh ./SpecRunner.html in your browser and check to see if the problem's test(s) are passing.
  3. Passed tests will be indicated by a green circle.
  4. Failed tests will be indicated by a red X.
  5.  
  6. You can refresh the page at any time to re-run all the tests.
  7. */
  8.  
  9. ////////// PROBLEM 1 //////////
  10.  
  11. // Do not edit the code below.
  12. var arr = [10,20,30];
  13. // Do not edit the code above.
  14.  
  15. /*
  16. Create a function named 'first' that is given 'arr' as an argument.
  17. Return the first item in the given array.
  18. */
  19.  
  20. //Code Here
  21. first = arr => arr[0];
  22.  
  23.  
  24. ////////// PROBLEM 2 //////////
  25.  
  26. // Do not edit the code below.
  27. var arr = [40,50,60];
  28. // Do not edit the code above.
  29.  
  30. /*
  31. Create a function named 'last' that is given 'arr' as an argument.
  32. Return the last item in the given array.
  33. */
  34.  
  35. //Code Here
  36. last = arr => arr[arr.length - 1]
  37.  
  38.  
  39. ////////// PROBLEM 3 //////////
  40.  
  41. // Do not edit the code below.
  42. var family = ['Tyler', 'Jordyn', 'Ryan', 'Chelsey', 'Ireland'];
  43. // Do not edit the code above.
  44.  
  45. /*
  46. Create a function named 'looper' that is given family as it's only argument.
  47. Loop through the given array and alert every item in the array.
  48. */
  49.  
  50. //Code Here
  51. looper = family => {
  52. for(i=0; i<family.length; i++){
  53. alert(family[i])
  54. }
  55. }
  56.  
  57.  
  58. ////////// PROBLEM 4 //////////
  59.  
  60. // Do not edit the code below.
  61. var letters = ['A', 'B', 'C', 'D', 'E'];
  62. // Do not edit the code above.
  63.  
  64. /*
  65. Write a function called reversedLooper that is given letters as it's only argument.
  66. Loop through the given array backwards alerting every item in the array starting at the end.\
  67. */
  68.  
  69. //Code Here
  70. reversedLooper = letters => {
  71. for(i = letters.length -1; i >= 0; i--){
  72. alert(letters[i])
  73. }
  74. }
  75.  
  76.  
  77. ////////// PROBLEM 5 //////////
  78.  
  79. // Do not edit the code below.
  80. var nums = [1,2,3,6,22,98,45,23,22,12];
  81. // Do not edit the code above.
  82.  
  83. /*
  84. Write a function named evenFinder that is given nums as it's only argument.
  85. Return an array that contains the even numbers from the nums array.
  86. */
  87.  
  88. //Code Here
  89. evenFinder = nums => nums.filter(e => e % 2 === 0)
  90.  
  91.  
  92. ////////// EXTRA PRACTICE PROBLEMS BELOW //////////
  93.  
  94. ////////// PROBLEM 6 //////////
  95.  
  96. // Do not edit the code below.
  97. var numbersArray = [1,2,34,54,55,34,32,11,19,17,54,66,13];
  98. // Do not edit the code above.
  99.  
  100. /*
  101. Write a function called divider that is given one argument, numbersArray.
  102. Have divider return an Array with the first item in the array being the evens array (all the even values from numbersArray) and the second item in the Array being the odds array (all the odd values from numbersArray).
  103. */
  104.  
  105. //Code Here
  106. divider = numbersArray => {
  107. var odds = []
  108. var evens = []
  109. var results = []
  110. for(var i = 0; i < numbersArray.length; i++){
  111. if(numbersArray[i] % 2 === 0) evens.push(numbersArray[i])
  112. if(numbersArray[i] % 2 !== 0) odds.push(numbersArray[i])
  113. }
  114. results.push(evens, odds)
  115. return results
  116. }
  117.  
  118.  
  119. ////////// PROBLEM 7 //////////
  120.  
  121. // Do not edit the code below.
  122. var getRandomArbitrary = function() {
  123. return Math.floor(Math.random() * 30);
  124. };
  125. // Do not edit the code above.
  126.  
  127. /*
  128. var numbers = [0,3,4,5,6,7,9,14,17,24,25,26,29,30];
  129. Above you're given a function that will return a random number between 0 and 30. There is also a commented out array full of numbers to help you visualize what your function will be receiving.
  130. Write a function named finder that will take in an array as an argument.
  131. It will then get a random number (by invoking getRandomArbitrary).
  132. Loop through the array to see if that random number is in the array.
  133. If it is, return true, if it's not, return false
  134. */
  135.  
  136. //Code Here
  137. finder = arr => {
  138. var num = getRandomArbitrary()
  139. return arr.includes(num)
  140. }
  141.  
  142.  
  143. ////////// PROBLEM 8 //////////
  144.  
  145. // Do not edit the code below.
  146. var myGroceryList = ['chips', 'pizza', 'hotpockets', 'MtnDew', 'corndogs'];
  147. // Do not edit the code above.
  148.  
  149. /*
  150. Here we're going to write a function that mimics going shopping and checking things off of our grocery list and adding new items to our list.
  151.  
  152. Write a function called removeItem that is given two arguments, the first is myGroceryList, and the second is an item to remove from myGroceryList.
  153. If the second argument (or the item to add or remove) matches an item in myGroceryList, remove that item from the your grocery list and return the new,
  154. updated grocery list.
  155.  
  156. Once you do that, write another function called addItem that is given two arguments, the first is myGroceryList and the second is an item to add to your grocery list.
  157. In addItem add the item you passed in to myGroceryList then return the new, updated grocery list.
  158.  
  159. In both removeItem and addItem check to see if the 'myGroceryList' and 'item' arguments are truthy.
  160. If they are not, return an empty array.
  161.  
  162. Here are some examples of calling your functions and what should be returned:
  163. removeItem(myGroceryList, 'chips') --> ['pizza', 'hotpockets', 'MtnDew', 'corndogs'];
  164. addItem(myGroceryList, 'Jerky') --> ['pizza', 'hotpockets', 'MtnDew', 'corndogs', 'Jerky'];
  165. removeItem(myGroceryList) --> [];
  166. addItem() --> [];
  167. */
  168.  
  169. //Code Here
  170. removeItem = (myGroceryList, item) => {
  171. return myGroceryList && item ? myGroceryList.filter(e => e !== item) : []
  172. }
  173. addItem = (myGroceryList, item) => {
  174. return myGroceryList && item ? myGroceryList.concat(item) : []
  175. }
  176.  
  177.  
  178. ////////// PROBLEM 9 //////////
  179.  
  180. /*
  181. Write a function called maker that creates an array, fills that array with numbers from 1 to 215, then returns the array.
  182. */
  183.  
  184. //Code Here
  185. maker = () => {
  186. var arr = []
  187. for(var i = 1; i < 216; i++){
  188. arr.push(i)
  189. }
  190. return arr;
  191. }
  192.  
  193.  
  194.  
  195. ////////// PROBLEM 10 //////////
  196.  
  197. // Do not edit the code below.
  198. var numbers = [5, '9', 16, 19, '25', '34', 48];
  199. // Do not edit the code above.
  200.  
  201. /*
  202. Write a function called addTen that is given 'numbers' as it's only argument.
  203. Return a new array after adding ten to each item in numbers.
  204. *Verify your answer is correct. --> [15, 19, 26, 29, 35, 44, 58]
  205. */
  206.  
  207. addTen = numbers => numbers.map(e => parseInt(e) + 10)
  208.  
  209.  
  210.  
  211. ////////// PROBLEM 11 //////////
  212.  
  213. // Do not edit the code below.
  214. var num1 = Math.floor(Math.random() * 30);
  215. var num2 = Math.floor(Math.random() * 30);
  216. var arr1 = [];
  217. var arr2 = [];
  218. for(var i = 0; i < num1; i++){
  219. arr1.push(i);
  220. }
  221. for(var i = 0; i < num2; i++){
  222. arr2.push(i);
  223. }
  224. // Do not edit the code above.
  225.  
  226. /*
  227. Above is some code that adds a random number of values to both arr1 and arr2.
  228. Write a function called 'longer' that is given arr1 and arr2 as it's only arguments.
  229. Return the array which is longest.
  230. */
  231.  
  232. //Code Here
  233. longer = (arr1, arr2) => arr1 > arr2 ? arr1 : arr2
  234.  
  235.  
  236. /*
  237. As a continuation of the previous problem, write another function called 'both'.
  238. Your 'both' function will be given two arguments, arr1 and arr2 (from the previous example).
  239. 'both' should return a new array with the matching numbers found in both arr1 and arr2.
  240.  
  241. Example: var arr1 = [1,2,3,4]; var arr2 = [2,4,5,6]; newArray // [2,4]
  242. */
  243.  
  244. //Code Here
  245. both = (arr1, arr2) => {
  246. var longer, shorter;
  247. longer = arr1 > arr2 ? arr1 : arr2
  248. shorter = arr1 < arr2 ? arr1 : arr2
  249.  
  250. var newArr = []
  251. for(var i = 0; i < longer.length; i++){
  252. for(var j = 0; j < shorter.length; j++){
  253. if (longer[i] === shorter[j]) {
  254. newArr.push(longer[i])
  255. }
  256. }
  257. }
  258. return newArr
  259. }
  260.  
  261.  
  262. ////////// PROBLEM 12 //////////
  263.  
  264. // Do not edit the code below.
  265. var devMountainEmployees = [];
  266.  
  267. var tyler = {
  268. name: 'Tyler',
  269. position: 'Lead Instructor/Engineer',
  270. spiritAnimal: 'Honey Badger'
  271. };
  272.  
  273. var cahlan = {
  274. name: 'Cahlan',
  275. position: 'CEO',
  276. spiritAnimal: 'butterfly'
  277. };
  278.  
  279. var ryan = {
  280. name: 'Ryan',
  281. position: 'Marketing',
  282. spiritAnimal: 'fox'
  283. };
  284.  
  285. var colt = {
  286. name: 'Colt',
  287. position: 'Everything really',
  288. spiritAnimal: 'Young Male Horse'
  289. };
  290. // Do not edit the code above.
  291.  
  292. /*
  293. Above you're given an empty array and four variables containing objects.
  294. Fill the devMountainEmployees array with those four objects.
  295. After that console.log the length of the Array and make sure that it's equal to 4.
  296. */
  297.  
  298. //Code Here
  299.  
  300.  
  301.  
  302. /*
  303. Now let's say Cahlan has a mental breakdown and has to take a leave of absence to 'find himself'.
  304. Loop through your devMountainEmployees until you find cahlan, then remove him from the array.
  305. */
  306.  
  307. //Code Here
  308.  
  309.  
  310.  
  311. ////////// PROBLEM 13 //////////
  312.  
  313.  
  314. /*
  315. A very clean way to pass around large LISTS (arrays) of COLLECTIONS (objects) of Data is to have an Array full of objects.
  316. Create an empty array called users.
  317. */
  318.  
  319. //Code Here
  320.  
  321.  
  322.  
  323. /*
  324. Now add three user objects to your users array. Each user object should contain the following properties. name, email, password, username.
  325.  
  326. Include the following user1 object as one of the objects in your array.
  327. */
  328.  
  329. // Do not edit the code below.
  330. var user1 = {
  331. name: 'Tyler McGinnis',
  332. email: 'tylermcginnis33@gmail.com',
  333. password: 'iLoveJavaScript',
  334. username: 'infiniteLoop'
  335. };
  336. // Do not edit the code above.
  337.  
  338. //Code Here
  339.  
  340.  
  341.  
  342. /*
  343. Now you have a very common data structure.
  344. Twitter is a good use case.
  345. It's easy to imagine that your followers list on Twitter is an Array full of objects and those objects contain properties about the specific person you follow.
  346.  
  347. Now let's say that Tyler decided to delete his account.
  348. Loop through your array of objects until you find Tyler's account (use tylermcginnis33@gmail.com to find him).
  349. Once you find the particular index he's located in, delete him from the array.
  350. */
  351.  
  352. //Code Here
  353.  
  354.  
  355.  
  356. /*
  357. The activity we just did is very much how data works in 'the real world'.
  358. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement