addTwo
that accepts one input and adds 2 to it.
addS
that accepts one input and adds an "s" to it.
map
that takes two inputs:map
return 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
forEach
takes an array and a callback, and runs the callback on each element of the array.
forEach
does not return anything.
let alphabet = ''; const letters = ['a', 'b', 'c', 'd']; forEach(letters, char => alphabet += char); console.log(alphabet); //prints 'abcd'
map
as mapWith
. This time you're
going to use forEach
inside of mapWith
instead of using a for
loop.
reduce
takes 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
initialValue
and
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.
intersection
that compares input arrays and returns a new array with elements found
in all of the inputs. BONUS: Use reduce!
union
that 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!
objOfMatches
that accepts two arrays and a callback. objOfMatches
will
build an object and return it. To build the object, objOfMatches
will 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.
multiMap
that will accept two arrays: an array of values and an array of callbacks.
multiMap
will 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.
commutative
that accepts two callbacks and a value. commutative
will 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).
objFilter
that accepts an object and a callback. objFilter
should 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. objFilter
will return this new object.
rating
that accepts an array (of functions) and a value. All the functions in the array will return true
or false
. rating
should return the percentage of functions from the array that return true
when the value is used as input.
pipe
that accepts an array (of functions) and a value. pipe
should 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. pipe
should return the final output.
highestFunc
that accepts an object (which will contain functions) and a subject (which is any value). highestFunc
should 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.
createFunction
that 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');
createFunctionPrinter
that 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');
outer
function. Notice that we are returning a function and that function is using variables that are outside of its scope.addByX
that returns a function that will add an input by x
.
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
once
that 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.
after
that 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.
delay
that 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();
russianRoulette
that 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 - 1
number of times it is invoked. On the very next invocation (the nth
invocation), the returned function will return the string 'bang'. On every invocation after that, the returned function returns the string 'reload to play again'.
average
that 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.
makeFuncTester
that accepts an array (of two-element sub-arrays), and returns a function (that will accept a callback). The returned
function should return true
if 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 false
.
makeHistory
that 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 functionblackjack
that 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:
blackjack
PLUS 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.
BONUS: Implement 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.
functionValidator
that accepts an array of functions and two different values (let's call them input
and 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!
allClear
that 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).
numSelectString
that 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.
movieSelector
that 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.
curriedAddThreeNums
that adds three numbers together when run thrice in succession as follows:
curriedAddThreeNums(1)(3)(7) //should return 10
curriedAddThreeNums
to create a new function curriedAddTwoNumsToFive
that when run twice in succession, adds two numbers to five as follows:
curriedAddTwoNumsToFive(6)(7) //should return 18