- Create a Higher order function map, which takes two arguments
- An array: arr
- A callback function: cb
- The map function creates a new array with the results of calling a provided function on every element in the calling array.
- Example:
- function square(n) { return n * n; }
- var arr = [1, 2, 3, 4];
- var arrSquares = map(arr, square); // [1, 4, 9, 16]
- Create a Higher order function filter, which takes two arguments
- An array: arr
- A callback function: cb
- The filter function creates a new array with all elements that pass the test implemented by the provided function.
- Example:
- function isEven(n) { return n % 2 == 0; }
- var arr = [1, 2, 3, 4];
- var arrEven = filter(arr, isEven); // [2, 4]
- Create a Higher order function reduce, which takes three arguments,
- An array: arr
- A callback function: cb
- An initial value: init
- The reduce function applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.
- Example:
- function sum(x, y) { return x + y; }
- var arr = [1, 2, 3, 4];
- var sum = reduce(arr, sum, 0); // 10
- Create a Higher order function find, which takes two arguments
- An array: arr
- A callback function: cb
- The find function returns the value of the first element in the provided array that satisfies the provided testing function.
- Example:
- function isEven(n) { return n % 2 == 0; }
- var arr = [1, 2, 3, 4];
- var firstEven = find(arr, isEven); // 2
- Create a Higher order function findIndex, which takes two arguments
- An array: arr
- A callback function: cb
- The findIndex function returns the index of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, -1 is returned.
- Example:
- function isEven(n) { return n % 2 == 0; }
- var arr = [1, 2, 3, 4];
- var firstEvenIndex = findIndex(arr, isEven); // 1
- Create a Higher order function every, which takes two arguments
- An array: arr
- A callback function: cb
- The every function tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.
- Example:
- function isEven(n) { return n % 2 == 0; }
- var arr = [1, 2, 3, 4];
- var allEven = every(arr, isEven); // false
- Create a Higher order function some, which takes two arguments
- An array: arr
- A callback function: cb
- The some function tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value.
- Example:
- function isEven(n) { return n % 2 == 0; }
- var arr = [1, 2, 3, 4];
- var someEven = some(arr, isEven); // true
-
Notifications
You must be signed in to change notification settings - Fork 7
JavaScriptADI/javascript191-assignment-8-assignment-functions-2
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
About
javascript191-assignment-8-assignment-functions-2 created by GitHub Classroom
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published