JavaScript Array Practice
These exercises are designed to reflect things you might encounter during a technical interview!
It's likely, and recommended, that you will want to come back to these from time to time for review.
Level 1
- Define a function that takes a string and an integer and creates a new array of entries equal to that integer.
Each value is equal to the string passed in.myFunction('sunshine', 3)
should return['sunshine', 'sunshine', 'sunshine']
;
Solution
- Define a function that takes an array and reverses all the values in an array
The function should not mutate the original array.
[1, 2, 3, 4, 5]
should return[5, 4, 3, 2, 1]
Solution
- Define a function that takes an array and removes all falsy values from the array
Solution
Level 2
-
Define a function that takes an array of nested arrays and returns an object composed of propeties equal to each nested array
For example:
const myArray = [['name', 'Charlie'], ['color', 'brown'], ['age', 10]];
would return
{ name: 'Charlie', color: 'brown', age: 10 };
Solution
-
Define a function that takes an array and removes duplicate values
HINT: Lookup
indexOf
- i.e.
[1,2,3,4,5,4,3]
should return[1,2,3,4,5]
- i.e.
Solution
-
Define a function that takes two arrays and returns true if they have identical values (order does not matter), it should return false otherwise
[1,2,3,4]
and[1,2,3,4]
should returntrue
[1,2,3,4,5]
and[1,2,3,4]
should returnfalse
[1,2,3,4]
and[1,2,3,4,4]
should returnfalse
[1,2,3,4]
and[1,4,3,2]
should returntrue
Solution
Level 9000
-
Define a function that takes an array and returns a new array with all sub-array elements concatenated into it (also known as "flattening" the array).
[0, 1, 2, [3, 4]]
should return[0, 1, 2, 3, 4]
[0, 1, 2, [[[3, 4]]]]
should return[0, 1, 2, 3, 4]
DO NOT USE Array.prototype.flat()