Advertisement
coletucker12

paradigmshw8

Nov 22nd, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ////////////////////////////////////////////////////////////
  2. //  CSCE 3193 Programming Paradigms
  3. //  Fall 2019
  4. //  Assignment 8
  5. //  Name:  
  6. ////////////////////////////////////////////////////////////
  7.  
  8.  
  9. //  You must complete the implementation of the six functions below.
  10. //  In all cases where it is given that the arguments to a function are
  11. //  numbers (or an array of numbers), you can assume that the correct types
  12. //  of values are passed as arguments and don't have to check for the wrong
  13. //  types or invalid input in the script used for testing.
  14.  
  15.  
  16. ////////////////////////////////////////////////////////////
  17. //  Recursion
  18.  
  19. function reverseArray(arr) {
  20.     //  Write the body of reverseArray so that it takes an arbitrary
  21.     //  array "arr" as an argument and returns an array with the elements
  22.     //  of arr in the exact opposite order.  For example, if arr = [1,2,3,4]
  23.     //  then this function must return the array [4,3,2,1].
  24.     //  THIS FUNCTION MUST USE RECURSION OR 60% WILL BE DEDUCTED.  Also, for
  25.     //  full credit you must not define inner functions (i.e. reverseArray must
  26.     //  be the recursive function) and it must not use more than one parameter.
  27.     if (!arr.length) { return arr; }
  28.     return reverseArray(arr.slice(1)).concat(arr[0]);
  29. }
  30.  
  31. function findMin(a) {
  32.     //  Write the body of findMin so that it takes a single argument
  33.     //  which is an array of integers (whose length can be assumed
  34.     //  to be at least 1). findMin should return the smallest integer
  35.     //  in the input array.
  36.     //  For example, findMin([8,22,4,58,481]) should return 4, and
  37.     //  findMin([23,4438,23,5,14,3,583,24,7]) should return 3.
  38.     //  THIS FUNCTION MUST USE RECURSION OR 60% WILL BE DEDUCTED
  39.     //  (e.g. there must not be any loops).  Also, for full credit
  40.     //  you must not define inner functions (i.e. findMin must be
  41.     //  the recursive function) and it must not use more than one parameter.
  42.     //  See http://www.w3schools.com/jsref/jsref_obj_array.asp for useful
  43.     //  array functions in JavaScript.
  44.     if (a.length === 1) { return a[0]; }
  45.     return findMin(a.slice(2).concat(a[0] < a[1] ? a[0]:a[1]);
  46. }
  47.  
  48. function stringFromArrays(arr1, arr2) {
  49.     //  Write the body of stringFromArrays so that it takes two arguments
  50.     //  which can be assumed to be two arrays of equal length containing strings
  51.     //  and/or numbers. stringFromArrays should return a single string formed
  52.     //  by concatenating the interleaved elements of the two arrays (treating
  53.     //  all elements as strings).
  54.     //  For example, stringFromArrays(["b",221,4],[1,"foo",22]) should return
  55.     //  "b1221foo422" and stringFromArrays([1,"a",3],[2,"b",4]) should return "12ab34".
  56.     //  THIS FUNCTION MUST USE RECURSION OR 60% WILL BE DEDUCTED
  57.     //  (e.g. there must not be any loops).  Also, for full credit
  58.     //  you must not define inner functions (i.e. stringFromArrays must be
  59.     //  the recursive function) and it must not use more than one parameter.
  60.     //  See http://www.w3schools.com/jsref/jsref_obj_array.asp for useful
  61.     //  array functions in JavaScript.
  62.     if (arr1.length === 1) { return arr1[0]; }
  63.     return '';
  64. }
  65.  
  66.  
  67. ////////////////////////////////////////////////////////////
  68. //  Higher order functions
  69.  
  70. function applyToArray(a) {
  71.     //  Write the body of applyToArray so that it takes a single
  72.     //  argument which is an array and returns a new function.
  73.     //  
  74.     //  Input: The argument 'a' can be assumed to be an array
  75.     //  (which may or may not be empty).
  76.     //
  77.     //  Output: applyToArray must return a function which itself takes a
  78.     //  single argument which can be assumed to be a function (which
  79.     //  we will call 'f' here). 'f' can be assumed to take a single
  80.     //  argument and return a single value.  The function returned
  81.     //  by applyToArray should return an array which is the result of
  82.     //  applying the function f individually to each element of array a.
  83.     //  
  84.     //  Examples: Given the definitions of add2 and mult4 at the bottom
  85.     //  of this page, if var f1 = applyToArray([1,2,3,4]), then f1(add2)
  86.     //  returns [3,4,5,6], if var f2 = applyToArray([10,-20,8,0]),
  87.     //  f2(mult4) returns [40,-80,32,0], and if var f3 = applyToArray([]),
  88.     //  f3(add2) returns []
  89. }
  90.  
  91. function computeMaxArr(f1, f2) {
  92.     //  Write the body of computeMaxArr so that it takes two functions as arguments
  93.     //  and returns a new function.
  94.     //
  95.     //  Input: Arguments f1 and f2 are assumed to be functions which each take
  96.     //  a single integer argument and return a single integer.
  97.     //
  98.     //  Output: The function returned by computeMaxArr must take a single argument,
  99.     //  an array of integers, and return an array which is the result of
  100.     //  applying either f1 or f2 to each of the elements, where the
  101.     //  the selection of f1 or f2 for each element is determined by which one
  102.     //  returns the larges number.  So, for a three element array arr, the output
  103.     //  would be the array consisting of:
  104.     //  [max(f1(arr[0]), f2(arr[0])), max(f1(arr[1]), f2(arr[1])), max(f1(arr[2]), f2(arr[2]))]
  105.     //  THIS FUNCTION MUST USE RECURSION OR 30% WILL BE DEDUCTED
  106.     //  (e.g. there must not be any loops).
  107.     //
  108.     //  Example: Given the definitions of add2 and mult4 at the bottom of
  109.     //  this page, if var fx = computeMaxArr(add2, mult4), then
  110.     //  fx([-3,-2,-1,0,1,2,3]) = -1,0,1,2,4,8,12
  111. }
  112.  
  113.  
  114. ////////////////////////////////////////////////////////////
  115. //  Common closure
  116.  
  117. function makeClosure() {
  118.     //  Write the body of makeClosure so that it takes no arguments and
  119.     //  returns an array of exactly two functions.  These two functions must be
  120.     //  contained within the same closure.  The closure should contain two variables
  121.     //  which are accessible to both functions.  The first should be called "counter"
  122.     //  and should be initialized to 0.  The second should be called "arr" and should
  123.     //  be initialized to an empty array.
  124.     //  Assuming that the variable "funcs" was used to store the return value from
  125.     //  makeClosure, (i.e."var funcs = makeClosure();"), then the
  126.     //  first function in funcs, which can be referenced as funcs[0], must take exactly
  127.     //  two numbers as arguments (arg1 and arg2).  If the value of "counter" is less
  128.     //  than 3, it should add (arg1 + arg2) and put that sum onto the end of the array "arr".
  129.     //  Otherwise, it should not change "arr".  Then, it should return the array "arr".
  130.     //  The second function returned from makeClosure, funcs[1] from the above
  131.     //  example, must take no arguments and simply increment the value of "counter" by 1
  132.     //  and return nothing.
  133.  
  134.     //  Example:  If the following series of function calls were made, the correct resulting
  135.     //  output is given.
  136.     //
  137.     //  var funcs = makeClosure();
  138.     //  outStr = funcs[0](1, 2) + "<br>";           // outStr will equal "3"
  139.     //  outStr = funcs[0](3, 4) + "<br>";           // outStr will equal "3, 7"
  140.     //  funcs[1]();                                 // counter will now equal 1
  141.     //  funcs[1]();                                 // counter will now equal 2
  142.     //  outStr = funcs[0](5, 6) + "<br>";           // outStr will equal "3, 7, 11"
  143.     //  funcs[1]();                                 // counter will now equal 3
  144.     //  outStr = funcs[0](7, 8) + "<br>";           // outStr will equal "3, 7, 11"
  145. }
  146.  
  147. ////////////////////////////////////////////////////////////
  148. //  Auxiliary functions (used for testing)
  149.  
  150. function mult4(i) {
  151.     return i * 4;
  152. }
  153.  
  154. function add2(j) {
  155.     return j + 2;
  156. }
  157.  
  158. function square(k) {
  159.     return k * k;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement