- Given an array of numbers, write a function that returns a new array where every element is incremented by 10.
Input: [1,2,3,4,5]
Output: [11,12,13,14,15]
- Given an array, return a new array where every non-string element is converted to an empty string.
Input: ["a", 123, "b", "c", {name: "cat"}]
Output: ["a", "", "b", "c", ""]
- Write a function that returns only the even elements in an array of numbers.
Input: [1,2,3,4,5]
Output: [2,4]
- Write a function that checks if all elements in the array are the same.
Input: [1,1,1,1,1]
Output: true
Input: [1,1,1,1,2]
Output: false
- Write a function that returns the sum of every element in an array.
Input: [1,2,3,4,5]
Output: 15
- Write a function that finds largest number in an array.
Input: [1,2,6,3,4,5]
Output: [6]
- Write a function
countZeroes
, which takes an array of numbers as its argument and returns the amount of zeroes that occur in it.
Input: [1,0,0,2,0,3,0,0,0]
Output: 6
- Write a function that doubles every element in an array.
Input: [1,2,3,4]
Output: [2,4,6,8]
- Write a function that finds the sum of every element in an array doubled
Input: [1,2,3,4]
Output: 20
- Write a function
elementDivisibleBy
with parametersdivisor
andarr
. Return a new array of every element of arr that can be evenly divided by divisor.
Input: 5, [4,5,6,7,8,9,10,11,12,13,14,15]
Output: [5,10,15]
- Write a
numberTimesIdx
that usesmap
and multiples each number in the array by its index.
Input: [6,7,8,9]
Output: [0,7,16,27]
- Write a function that returns whether or not every value in an array is a positive even number that doesn't end in 0.
Input: [2,4,6,8,12]
Output: true
Input: [2,4,6,8,10]
Output: false
- Write a function that returns the sum of all odd numbers in an Array
Hint: What is the second argument in reduce
?
Input: [2,3,4,5]
Output: 8
Input: [1,2,3,4,5]
Output: 9
- Write a function that returns a new array containing all of the strings in the original array uppercased.
Input: [{}, 1, "cat", 3, ["hi"], {name: "dog"}, "dog", "bear"]
Output: ["CAT", "DOG", "BEAR"]
You can write your own array methods!
Array.prototype.sayHi = function() {
console.log("Hi")
}
Now every array that you write has a sayHi
method that you can call.
let myArr = [1,2,3]
myArr.sayHi() //Logs "Hi" to the console
-
Using Array.prototype add a new method
myForEach
that mimics the behavior of the built inforEach
. -
Use your
myForEach
to addmyMap
to the Array prototype.myMap
should behave the same as regularmap
. -
Write a
myFilter
that behaves the same asfilter
. -
Write a
myEvery
that behaves the same asevery
. -
Write a
myReduce
. It should use the first element in the array if none is given. -
Write a
myJoin
function. -
Write a
mySlice
function. -
Write a
myTranspose
. This function should transpose a matrix.
let mtx = [
[1, 2],
[3, 4],
[5, 6]
]
mtx.myTranspose();
// => [
[1, 3, 5],
[2, 4, 6]
]
- Write a
myFlatten
. This should take a multi-dimensional array and return it as one array.
let arr = [1, 2, [3, 4, 5, [6, 7, 8]]]
arr.myFlatten();
// => [1, 2, 3, 4, 5, 6, 7, 8]