-
Determine what these functions do and give them a good name:
const f1 = (n) => [...`${n}`].map((i) => parseInt(i)); const f2 = (...nums) => nums.reduce((acc, val) => acc + val, 0) / nums.length; const f3 = (arr, val) => arr.reduce((a, v) => (v === val ? a + 1 : a), 0); const f4 = (A) => A.filter((item, index) => index === arr.indexOf(item)); const f5 = (A) => [].concat(...A);
-
Write a function
count
that, given an array of numbers and a callback, counts the number of items for which the callback returnstrue
. -
Implement two functions,
map
andfilter
(receiving an array and a callback), in terms offorEach
. -
Likewise, implement
find
in terms offilter
(ignore efficiency!). -
Implement
some
andevery
in terms ofmap
andfilter
. -
Write a function
singles
that, given an array of numbers, returns a new array containing only the values of the original array that appear only once. Implement it in terms offilter
. -
Write a function
uniq
that, given an array of numbers, removes consecutive repetitions of values. -
Implement the
uniq
function of the last exercise in terms ofreduce
. -
Sort an array of objects such as
{ name: 'Fernando', lastname: 'Rodríguez', age: 39 }
by lastname
-
Write the
findIndex
method as a functionfunction myFindIndex(array, fn) { /* ... */ }
-
Write a function
rotateArray(array, n)
which rotates an array by n positions. For example:rotateArray([1, 2, 3, 4, 5], 2)
will return[4, 5, 1, 2, 3]
. -
Write a function which filters and array keeping only the elements that have the fields
x
andy
.