Arrays Flashcards

1
Q

What is an ordered collection of values?

A

An Array

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

What is an element of an array?

A

Each value in the array

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

What are array indexes?

A

It is the numeric position of the element in the array

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

What does it mean to 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

Can array elements be objects or other arrays?

A

Yes

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

What are some properties of Arrays in JavaScript?

A
  • Ordered collection of values
  • Specialized form of Object (array indexes are like properties)
  • Arrays are dynamic: grow or shrink as needed
  • JavaScript arrays are untyped
  • Array elements may be objects or other array
  • Are zero-based and use 32-bit indexes (Highest posible index 2↑32 - 2)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does it mean that JavaScript Arrays may be sparse?

A

The elements not need to have contiguous indexes, and ther may be gaps.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
let array = [10,2,,5,1]

What is the .length?

A

5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
let array = [,,]

What is the .length?

A

2

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

What are the four ways to create arrays?

A
  • Array literals
  • … spread operator
  • Array() constructor
  • Array.of() and Array.from() factory methods
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is an Array literal?

A

comma-separated list of array elements within square brackets
~~~
let empty = [];
let primes = [2,3,4,7,11];
~~~

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

Is the following code valid? Why?
~~~
let base = 1024;
let table = [base, base + 1, base +2, base + 3];
~~~

A

It is valid because array literal values need NOT to be constants.

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

What does the following code print?

let array = [10,2,,5,1]
console.log(array[2]); 
A
undefined
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is missing the make the following code valid?

let a = [1, 2, 3];
let b = [0, ?, 4];
// b == [1, 2, 3, 4]
A

The spread operator in line 2
~~~
let a = [1, 2, 3];
let b = [0, …a, 4]; // notice …a
// b == [1, 2, 3, 4]
~~~

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

How to create a copy of the following array using the spread operator?

let original = [1, 2, 3];
A
let original = [1, 2, 3];
let copy = [...original];

Modifying the copy does not change the original

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

How can I turn the following string into an array of single-char array?

let letters = "abcde123";
A
let letters = "abcde123";
let arrayOfCharacters = [...letters]

Strings are iterable

Spread operator works on any iterable object

17
Q

What does the following code do?

let letters = [..."hello world"];
[...new Set(letters)]
A

It removes duplicates from the array resulting in:
~~~
// => [“h”, “e”, “l”, “o”, “ “, “w”, “r”, “d”]
~~~