addTwothat accepts one input and adds 2 to it.
addSthat accepts one input and adds an "s" to it.
mapthat takes two inputs:
mapreturn a new array filled with numbers that are the result of using the 'callback' function on each element of the input array.
map([1,2,3,4,5], multiplyByTwo); //-> [2,4,6,8,10]
multiplyByTwo(1); //-> 2
multiplyByTwo(2); //-> 4
forEachtakes an array and a callback, and runs the callback on each element of the array.
forEachdoes not return anything.
let alphabet = ''; const letters = ['a', 'b', 'c', 'd']; forEach(letters, char => alphabet += char); console.log(alphabet); //prints 'abcd'
mapWith. This time you're going to use
mapWithinstead of using a
reducetakes an array and reduces the elements to a single value. For example it can sum all the numbers, multiply them, or any operation that you can put into a function.
const nums = [4, 1, 3]; const add = (a, b) => a + b; reduce(nums, add, 0); //-> 8Here's how it works. The function has an "accumulator value" which starts as the
initialValueand accumulates the output of each loop. The array is iterated over, passing the accumulator and the next array element as arguments to the
callback. The callback's return value becomes the new accumulator value. The next loop executes with this new accumulator value. In the example above, the accumulator begins at 0.
add(0,4)is called. The accumulator's value is now 4. Then
add(4, 1)to make it 5. Finally
add(5, 3)brings it to 8, which is returned.
intersectionthat compares input arrays and returns a new array with elements found in all of the inputs. BONUS: Use reduce!
unionthat compares input arrays and returns a new array that contains all elements. If there are duplicate elements, only add it once to the new array. Preserve the order of the elements starting from the first element of the first input array. BONUS: Use reduce!
objOfMatchesthat accepts two arrays and a callback.
objOfMatcheswill build an object and return it. To build the object,
objOfMatcheswill test each element of the first array using the callback to see if the output matches the corresponding element (by index) of the second array. If there is a match, the element from the first array becomes a key in an object, and the element from the second array becomes the corresponding value.
multiMapthat will accept two arrays: an array of values and an array of callbacks.
multiMapwill return an object whose keys match the elements in the array of values. The corresponding values that are assigned to the keys will be arrays consisting of outputs from the array of callbacks, where the input to each callback is the key.
commutativethat accepts two callbacks and a value.
commutativewill return a boolean indicating if the passing the value into the first function, and then passing the resulting output into the second function, yields the same output as the same operation with the order of the functions reversed (passing the value into the second function, and then passing the output into the first function).
objFilterthat accepts an object and a callback.
objFiltershould make a new object, and then iterate through the passed-in object, using each key as input for the callback. If the output from the callback is equal to the corresponding value, then that key-value pair is copied into the new object.
objFilterwill return this new object.
ratingthat accepts an array (of functions) and a value. All the functions in the array will return
ratingshould return the percentage of functions from the array that return
truewhen the value is used as input.
pipethat accepts an array (of functions) and a value.
pipeshould input the value into the first function in the array, and then use the output from that function as input for the second function, and then use the output from that function as input for the third function, and so forth, until we have an output from the last function in the array.
pipeshould return the final output.
highestFuncthat accepts an object (which will contain functions) and a subject (which is any value).
highestFuncshould return the key of the object whose associated value (which will be a function) returns the largest number, when the subject is given as input.
createFunctionthat creates and returns a function. When that created function is called, it should print "hello".
const function1 = createFunction();When you think you completed createFunction, un-comment out those lines in the code and run it to see if it works.
// now we'll call the function we just created
function1(); //should console.log('hello');
createFunctionPrinterthat accepts one input and returns a function. When that created function is called, it should print out the input that was used when the function was created.
const printSample = createFunctionPrinter('sample'); const printHello = createFunctionPrinter('hello') // now we'll call the functions we just created printSample(); //should console.log('sample'); printHello(); //should console.log('hello');
outerfunction. Notice that we are returning a function and that function is using variables that are outside of its scope.
addByXthat returns a function that will add an input by
const addByTwo = addByX(2); addByTwo(1); //should return 3 addByTwo(2); //should return 4 addByTwo(3); //should return 5 const addByThree = addByX(3); addByThree(1); //should return 4 addByThree(2); //should return 5 const addByFour = addByX(4); addByFour(4); //should return 8 addByFour(10); //should return 14
oncethat accepts a callback as input and returns a function. When the returned function is called the first time, it should call the callback and return that output. If it is called any additional times, instead of calling the callback again it will simply return the output value from the first time it was called.
afterthat takes the number of times the callback needs to be called before being executed as the first parameter and the callback as the second parameter.
delaythat accepts a callback as the first parameter and the wait in milliseconds before allowing the callback to be invoked as the second parameter. Any additional arguments after wait are provided to func when it is invoked. HINT: research setTimeout();
russianRoulettethat accepts a number (let us call it
n), and returns a function. The returned function will take no arguments, and will return the string 'click' the first
n - 1number of times it is invoked. On the very next invocation (the
nthinvocation), the returned function will return the string 'bang'. On every invocation after that, the returned function returns the string 'reload to play again'.
averagethat accepts no arguments, and returns a function (that will accept either a number as its lone argument, or no arguments at all). When the returned function is invoked with a number, the output should be average of all the numbers have ever been passed into that function (duplicate numbers count just like any other number). When the returned function is invoked with no arguments, the current average is outputted. If the returned function is invoked with no arguments before any numbers are passed in, then it should return 0.
makeFuncTesterthat accepts an array (of two-element sub-arrays), and returns a function (that will accept a callback). The returned function should return
trueif the first elements (of each sub-array) being passed into the callback all yield the corresponding second elements (of the same sub-array). Otherwise, the returned function should return
makeHistorythat accepts a number (which will serve as a limit), and returns a function (that will accept a string). The returned function will save a history of the most recent "limit" number of strings passed into the returned function (one per invocation only). Every time a string is passed into the function, the function should return that same string with the word 'done' after it (separated by a space). However, if the string 'undo' is passed into the function, then the function should delete the last action saved in the history, and return that delted string with the word 'undone' after (separated by a space). If 'undo' is passed into the function and the function's history is empty, then the function should return the string 'nothing to undo'.
Inspect the commented out test cases carefully if you need help to understand these instructions.Create a function
blackjackthat accepts an array (which will contain numbers ranging from 1 through 11), and returns a DEALER function. The DEALER function will take two arguments (both numbers), and then return yet ANOTHER function, which we will call the PLAYER function.
On the FIRST invocation of the PLAYER function, it will return the sum of the two numbers passed into the DEALER function.
On the SECOND invocation of the PLAYER function, it will return either:
blackjackPLUS the sum of the two numbers passed in as arguments into the DEALER function, IF that sum is 21 or below, OR
You may assume that the given array is long enough to give a 'bust' before running out of numbers.
blackjack so the DEALER function can return more PLAYER functions that will each
continue to take the next number in the array after the previous PLAYER function left off. You will just need to
make sure the array has enough numbers for all the PLAYER functions.
functionValidatorthat accepts an array of functions and two different values (let's call them
output). This function should return a new array containing *only* the functions from the original array that, when invoked with
input, return the value
output. Use reduce!
allClearthat accepts an array of evaluator functions (each returning a boolean value), and a single value. Using reduce, return a single boolean value indicating whether the value "passes" every single one of the evaluator functions (i.e. returns true).
numSelectStringthat accepts an array of numbers and returns a string. This function should use filter, sort, and reduce to return a string containing only the odd numbers from the array, separated by commas, in ascending order.
movieSelectorthat accepts an array of objects containing movie information (id, title, and score). Chain together invocations of map, filter AND reduce to return an array containing only movies with a score greater than 5. The titles should be all uppercase strings.
curriedAddThreeNumsthat adds three numbers together when run thrice in succession as follows:
curriedAddThreeNums(1)(3)(7) //should return 10
curriedAddThreeNumsto create a new function
curriedAddTwoNumsToFivethat when run twice in succession, adds two numbers to five as follows:
curriedAddTwoNumsToFive(6)(7) //should return 18