Arrays Flashcards

1
Q

What is an array

A

An array is an ordered collection of values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How is each value of an array called ?

A

An element

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How is the position of a value in an array called ?

A

An index

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does it mean to say that javascript arrays are untyped.

A

An array element may be of any type, and different elements of the same array may be of different types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a sparse array ?

A

It is an array with gaps. Elements are not contiguous.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the different ways to create an array ?

A
  • Use Array Literals
  • Use the constructor Array()
  • Use Array.of
  • Use Array.from
  • Use the … spread operator on an iterable object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the length of this Array litteral and why ?

[,,]

A

This array litteral has a length of 2.

The last comma is considered as a trailing one. Not a separating comma.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

I which cases Array.of() should be used ?

A

Array.of() is a factory method that creates and returns a new array, using its argument values (regardless of how many of them there are) as the array elements:

Array.of() // => []; returns empty array with no arguments

Array.of(10) // => [10]; can create arrays with a single numeric argument

Array.of(1,2,3) // => [1, 2, 3]

It is comparable with the Array() constructor called with multiple arguments. The difference is that that Array() constructor cannot be used to create an array with a single numeric value specified

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

In which cases the length property of an Array is automatically updated ?

A
  • when you assign an element of the array using property names that are non-negative integers less than 232–1.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the difference between an array index and an array property name ?

A

All indexes are property names, but only property names that are integers between 0 and 232–2 are indexes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the particularity of the length property of an array when that array is sparse ?

A

If the array is sparse, the length property is no more the number of elements in the array. Rather, the value of the length property is greater than the number of elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the purpose of the method push() of arrays ?

A

To add one or more values to the end of an array:

let a = []; // Start with an empty array

a.push(“zero”); // Add a value at the end.

a = [“zero”]
a.push(“one”, “two”); // Add two more values.
a = [“zero”, “one”, “two”]

Pushing a value onto an array a is the same as assigning the value to a[a.length]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the purpose of the method unshift() of arrays ?

A

To insert a value at the beginning of an array, shifting the existing array elements to higher indexes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the purpose of the method pop() of arrays ?

A

The pop() method is the opposite of push(): it removes the last element of the array and returns it, reducing the length of an array by 1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the purpose of the method shift() of arrays ?

A

shift() method removes and returns the first element of the array, reducing the length by 1 and shifting all elements down to an index one lower than their current index.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the difference between the method forEach() of arrays and the loop for/of ?

A

You pass a function to the forEach() method of an array, and forEach() invokes your function once on each element of the array.

Unlike the for/of loop, the forEach() is aware of sparse arrays and does not invoke your function for elements that are not there.

forEach() does not provide a way to terminate iteration before all elements have been passed to the function. That is, there is no equivalent of the break statement you can use with a regular for loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the particularity of array Iterator methods when the array is sparse ?

A

If the array is sparse, the function you pass is not invoked for nonexistent elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

List the 3 arguments the forEach method (as well as some other Iterator methods) receive.

A

the value of the array element

The index of the array element.

And the array itself.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Describe the map() method of arrays

A

The map() method passes each element of the array on which it is invoked to the function you specify and returns an array containing the values returned by your function.

For example:

let a = [1, 2, 3];

a.map(x => xx) // => [1, 4, 9]: the function takes input x and returns xx

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Which differences exist between forEach() method of arrays and map() method ?

A

The function you pass to map() is invoked in the same way as a function passed to forEach(). For the map() method, however, the function you pass should return a value.

map() returns a new array: it does not modify the array it is invoked on. forEach() allIf that array is sparse, your function will not be called for the missing elements, but the returned array will be sparse in the same way as the original array: it will have the same length and the same missing elements

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Describe the method filter() of arrays

A

The filter() method returns an array containing a subset of the elements of the array on which it is invoked. The function you pass to it should be predicate: a function that returns true or false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How to close the gaps in sparse arrays using filter method ?

A

let dense = sparse.filter(() => true);

23
Q

What does the find() and findIndex() methods of arrays take as parameter ?

A

A function that is a predicate (that returns true or false) ans that receive as parameter an element of the array.

24
Q

What does the find() method of arrays return ?

A

Undefined or the matching element

25
Q

What does the findIndex() method of arrays return ?

A

-1 or the index of the matching element.

26
Q

What do the every() and some() methods of arrays takes as parameter ?

A

A predicate.

a.every(x => x < 10)

27
Q

What does the every() method of arrays return ?

A

True or false.

True if all the predicate calls have return true.
Otherwise false.

28
Q

What does the some() method of arrays return ?

A

True or False.

True if at least one element of the array will return true when passed to the predicate.

Otherwise false.

29
Q

What does every() return when called on an empty array?

A

True

30
Q

What does some() return when called on an empty array?

A

False

31
Q

Which difference exist between functions received as parameter with forEach(),map(),etc. And reduce() and reduceRight()

A

Functions used with reduce() are different than the functions used with forEach() and map(). The familiar value, index, and array values are passed as the second, third, and fourth arguments. The first argument is the accumulated result of the reduction so far. On the first call to the function, this first argument is the initial value you passed as the second argument to reduce(). On subsequent calls, it is the value returned by the previous invocation of the function.

32
Q

What happen when the 2nd argument of reduce is omitted ?

A

When you invoke reduce() like this with no initial value, it uses the first element of the array as the initial value. This means that the first call to the reduction function will have the first and second array elements as its first and second arguments.

33
Q

What do the reduce() and reduceRight() methods do ? What are the differences.

A

The reduce() and reduceRight() methods combine the elements of an array, using the function you specify, to produce a single value.

reduceRight() works just like reduce(), except that it processes the array from highest index to lowest (right-to-left), rather than from lowest to highest.

let a = [1,2,3,4,5];
a.reduce((x,y) => x+y, 0)

34
Q

What is the default initial value of reduce() when it’s second parameter is omitted ?

A

The first element of the array

35
Q

Which methods of arrays could be used to flatten arrays ?

A

flat() and flatMap()

[1, [2, 3]].flat() // => [1, 2, 3]

[1, [2, [3]]].flat() // => [1, 2, [3]]

let a = [1, [2, [3, [4]]]];
a.flat(1) // => [1, 2, [3, [4]]]
a.flat(2) // => [1, 2, 3, [4]]
a.flat(3) // => [1, 2, 3, 4]

let phrases = [“hello world”, “the definitive guide”];
let words = phrases.flatMap(phrase => phrase.split(“ “));
words // => [“hello”, “world”, “the”, “definitive”, “guide”];

36
Q

What does the flatMap method do ?

A

a.flatMap(f) is the same as :

a.map(f).flat()

37
Q

What does the concat method do ?

A

The concat() method creates and returns a new array that contains the elements of the original array on which concat() was invoked, followed by each of the arguments to concat().

If any of these arguments is itself an array, then it is the array elements that are concatenated, not the array itself (but not recursively)

38
Q

What is the danger of concat() ?

A

It creates a new array. The programmer should think about modifying the array in place with push or splice.

39
Q

How to add all elements of an array b at the end of another array a ?

A

a.push(…b)

40
Q

How does unshift() behave when it is passed multiple arguments ? like a.unshift(1, 2)

A

The arguments are inserted all at once, which means that they end up in the array in a different order than they would be if you inserted them one at a time.

a = []
a.unshift(1,2) => [1,2]

a = []
a.unshift(1); => [1]
a.unshift(2); => [2,1]

41
Q

Describe the slice() method of arrays

A

The slice() method returns a subarray of the specified array. Its first argument is the index where to start (included) and its second arguments is the index where to stop (excluded).

Negative arguments are possible. They are relative to the length of the array. -1 being the last element of the array.

the absolute index (the real index, not the relative one) of the start parameter (1st argument) must be lower than the absolute index of the stop parameter (2nd argument). Otherwise the array will be empty

42
Q

Describe the splice() method of arrays

A

The splice() method is used to delete some elements of the array, and also to insert (in place) elements into the array.

The first argument indicates the index (included) from which the deletion must start

The 2nd argument indicates the number of elements to delete, counting from the 1st argument. If it is 0, nothing will be deleted.

All the following elements are to be inserted in the array, starting from the first argument index.

43
Q

What does splice() returns ?

A

Splice returns an array of the deleted ements

44
Q

What does the fill() method do ?

A

The fill() method sets the elements of an array, or a slice of an array, to a specified value. It mutates the array it is called on, and also returns the modified array.

let a = new Array(5); // Start with no elements and length 5
a.fill(0) // => [0,0,0,0,0]; fill the array with zeros
a.fill(9, 1) // => [0,9,9,9,9]; fill with 9 starting at index 1
a.fill(8, 2, -1) // => [0,9,8,8,9]; fill with 8 at indexes 2, 3

The first argument to fill() is the value to set array elements to.

The optional second argument specifies the starting index. If omitted, filling starts at index 0.

The optional third argument specifies the ending index — array elements up to, but not including, this index will be filled. If this argument is omitted, then the array is filled from the start index to the end.

45
Q

What does the copyWithin() method do ?

A

copyWithin() copies a slice of an array to a new position within the array. It modifies the array in place and returns the modified array, but it will not change the length of the array.

The first argument specifies the destination index to which the first element will be copied.

The second argument specifies the index of the first element to be copied. If this second argument is omitted, 0 is used.

The third argument specifies the end of the slice of elements to be copied. If omitted, the length of the array is used.

Elements from the start index up to, but not including, the end index will be copied.

46
Q

What does the includes() method do ?

A

The ES2016 includes() method takes a single argument and returns true if the array contains that value or false otherwise. It does not tell you the index of the value, only whether it exists.

47
Q

What is the difference between the methods includes and indexOf() ?

A

includes() returns true or false
indexOf() returns -1 or the index of the first matching element

indexOf() use the strict equality operator to compare values of the array with the specified value and that equality algorithm considers the not-a-number value to be different from every other value, including itself.

includes() uses a slightly different version of equality that does consider NaN to be equal to itself.

This means that indexOf() will not detect the NaN value in an array, but includes() will:

48
Q

How does the sort() method of arrays work ?

A

sort() sorts the elements of an array in place and returns the sorted array. When sort() is called with no arguments, it sorts the array elements in alphabetical order (temporarily converting them to strings to perform the comparison, if necessary).

If an array contains undefined elements, they are sorted to the end of the array.

49
Q

How to modify the sort() algorithm so that it does no more an alphabetical sort ?

A

To sort an array into some order other than alphabetical, you must pass a comparison function as an argument to sort(). This function decides which of its two arguments should appear first in the sorted array.

If the first argument should appear before the second, the comparison function should return a number less than zero.

If the first argument should appear after the second in the sorted array, the function should return a number greater than zero.

And if the two values are equivalent (i.e., if their order is irrelevant), the comparison function should return 0.

50
Q

What does the reverse() method of Arrays do ?

A

The reverse() method reverses the order of the elements of an array and returns the reversed array. It does this in place; in other words, it doesn’t create a new array with the elements rearranged but instead rearranges them in the already existing array.

51
Q

What are the 3 static functions of Array ?

A
  • Array.of()
  • Array.from()
  • Array.isArray()
52
Q

What is an array like object ?

A

It is an object (type “object”), that has a length property that is an integer between 0 and 2^32 - 1. (and, normally some integer properties between 0 and length)

53
Q

How to call an array method on an Array like object ?

A

Using call():

Array.prototype.join.call(a, “+”) // => “a+b+c”
Array.prototype.map.call(a, x => x.toUpperCase()) // => [“A”,”B”,”C”]

Array.prototype.slice.call(a, 0) // => [“a”,”b”,”c”]: true array copy

Array.from(a) // => [“a”,”b”,”c”]: easier array copy