Array Flashcards

1
Q

const array = [1, 2, 3]

console.log(array.length)

A

// expected output: 3

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

const array1 = [5, 12, 8, 130, 44];

let index = 2;

console.log(Using an index of ${index} the item returned is ${array1.at(index)});

index = -2;

console.log(Using an index of ${index} item returned is ${array1.at(index)});

A

// expected output: “Using an index of 2 the item returned is 8”

// expected output: “Using an index of -2 item returned is 130”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
const a1 = [1, 2, 3]
const a2 = [4, 5, 6]
const a3 = [7, 8, 9]

const a4 = a1.concat(a2, a3)

console.log(a4)

A

immutable

// [1,2,3,4,5,6,7,8,9]

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

const array1 = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’];

console. log(array1.copyWithin(0, 3, 4));
console. log(array1.copyWithin(1, 3));

A

// copy to index 0 the element at index 3

// expected output: Array [“d”, “b”, “c”, “d”, “e”]

// copy to index 1 all elements from index 3 to the end

// expected output: Array [“d”, “d”, “e”, “d”, “e”]

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

const array1 = [‘a’, ‘b’, ‘c’];

const iterator1 = array1.entries();

console. log(iterator1.next().value);
console. log(iterator1.next().value);

A
// expected output: Array [0, "a"]
// expected output: Array [1, "b"]

does not mutate

var a = ['a', 'b', 'c'];
var iterator = a.entries();
for (let e of iterator1) { console.log(e); }
// [0, 'a']
// [1, 'b']
// [2, 'c']
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

const isBelowThreshold = (currentValue) => currentValue < 40;

const array1 = [1, 30, 39, 29, 10, 13];

console.log(array1.every(isBelowThreshold));

A

// expected output: true

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

const array1 = [1, 2, 3, 4];

console. log(array1.fill(0, 2, 4));
console. log(array1.fill(5, 1));
console. log(array1.fill(6));

A
// fill with 0 from position 2 until position 4
// expected output: [1, 2, 0, 0]
// fill with 5 from position 1
// expected output: [1, 5, 5, 5]

// expected output: [6, 6, 6, 6]

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

const words = [‘spray’, ‘limit’, ‘elite’, ‘exuberant’, ‘destruction’, ‘present’];

const result = words.filter(word => word.length > 6);

console.log(result);

A

// expected output: Array [“exuberant”, “destruction”, “present”]

// cb (item, index, array)

// accepts this arg

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

const array1 = [5, 12, 8, 130, 44];

const found = array1.find(element => element > 10);

console.log(found);

A

// expected output: 12

// cb(item, index, array)

// accepts this arg

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

const array1 = [5, 12, 8, 130, 44];

const isLargeNumber = (element) => element > 13;

console.log(array1.findIndex(isLargeNumber));

A

// expected output: 3

// cb(item, index, array)

// accepts this arg

// Arrow function
findIndex((element) =\> { ... } )
findIndex((element, index) =\> { ... } )
findIndex((element, index, array) =\> { ... } )

// Callback function
findIndex(callbackFn)
findIndex(callbackFn, thisArg)

// Inline callback
function findIndex(function callbackFn(element) { ... })
findIndex(function callbackFn(element, index) { ... })
findIndex(function callbackFn(element, index, array){ ... })
findIndex(function callbackFn(element, index, array) { ... }, thisArg)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

const arr1 = [0, 1, 2, [3, 4]];

console.log(arr1.flat());

const arr2 = [0, 1, 2, [[[3, 4]]]];

console.log(arr2.flat(2))

A

// expected output: [0, 1, 2, 3, 4]

// expected output: [0, 1, 2, [3, 4]]

// depth defaults to 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
let arr1 = [1, 2, 3, 4];
arr1.map(x =\> [x \* 2]);

arr1. flatMap(x => [x * 2]);
arr1. flatMap(x => [[x * 2]]);

A

// [[2], [4], [6], [8]]

// [2, 4, 6, 8]

// only one level is flattened

// [[2], [4], [6], [8]]

like calling map followed by .flat(1)

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

const array1 = [‘a’, ‘b’, ‘c’];

array1.forEach(element => console.log(element));

A
// expected output: "a"
// expected output: "b"
// expected output: "c"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

console. log(Array.from(‘foo’));
console. log(Array.from([1, 2, 3], x => x + x));

A

// expected output: Array [“f”, “o”, “o”]

// expected output: Array [2, 4, 6]

accepts and array-like or iterable

inline mapping
Array.from(arrayLike, mapFn)

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

const array1 = [1, 2, 3];

console.log(array1.includes(2));

const pets = [‘cat’, ‘dog’, ‘bat’];

console. log(pets.includes(‘cat’));
console. log(pets.includes(‘at’));

A

// expected output: true

// expected output: true

// expected output: false

accepts a second argument fromIndex

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

const beasts = [‘ant’, ‘bison’, ‘camel’, ‘duck’, ‘bison’];

console. log(beasts.indexOf(‘bison’));
console. log(beasts.indexOf(‘bison’, 2));
console. log(beasts.indexOf(‘giraffe’));

A

// expected output: 1

// start from index 2
// expected output: 4
// expected output: -1
17
Q

Array.isArray([1, 2, 3]);
Array.isArray({foo: 123});
Array.isArray(‘foobar’);
Array.isArray(undefined);

A
// true
// false
// false
// false
18
Q

const elements = [‘Fire’, ‘Air’, ‘Water’];

console. log(elements.join());
console. log(elements.join(‘’));
console. log(elements.join(‘-‘));

A

// expected output: “Fire,Air,Water”

// expected output: “FireAirWater”

// expected output: “Fire-Air-Water”

19
Q
const array1 = ['a', 'b', 'c'];
const iterator = array1.keys();

for (const key of iterator) {
console.log(key);
}

A
// expected output: 0
// expected output: 1
// expected output: 2
20
Q

const animals = [‘Dodo’, ‘Tiger’, ‘Penguin’, ‘Dodo’];

console. log(animals.lastIndexOf(‘Dodo’));
console. log(animals.lastIndexOf(‘Tiger’));
console. log(animals.lastIndexOf(‘Tiger’, 2));

A

// expected output: 3

// expected output: 1

// expected output: -1

21
Q

const array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map(x =\> x \* 2);

console.log(map1)

A

// expected output: Array [2, 8, 18, 32]

22
Q

Array.of(7);
Array(7);
Array.of(1, 2, 3);
Array(1, 2, 3);

A
// [7]
// array of 7 empty slots
// [1, 2, 3]
// [1, 2, 3]

Like array but fixes the since integer argument issue

23
Q

const plants = [‘broccoli’, ‘cauliflower’, ‘cabbage’, ‘kale’, ‘tomato’];

console. log(plants.pop());
console. log(plants);
plants. pop();
console. log(plants);

A

// expected output: “tomato”

// expected output: Array [“broccoli”, “cauliflower”, “cabbage”, “kale”]

// expected output: Array [“broccoli”, “cauliflower”, “cabbage”]

24
Q

const animals = [‘pigs’, ‘goats’, ‘sheep’];

const count = animals.push('cows');
console.log(count);

console.log(animals);

animals. push(‘chickens’, ‘cats’, ‘dogs’);
console. log(animals);

A

// expected output: 4

// expected output: Array [“pigs”, “goats”, “sheep”, “cows”]

// expected output: Array [“pigs”, “goats”, “sheep”, “cows”, “chickens”, “cats”, “dogs”]

25
``` const array1 = [1, 2, 3, 4]; const reducer = (accumulator, currentValue) =\> accumulator + currentValue; ``` ``` // 1 + 2 + 3 + 4 console.log(array1.reduce(reducer)); ``` ``` // 5 + 1 + 2 + 3 + 4 console.log(array1.reduce(reducer, 5)); ```
// expected output: 10 // expected output: 15
26
const array1 = [[0, 1], [2, 3], [4, 5]].reduceRight( (accumulator, currentValue) =\> accumulator.concat(currentValue) ); console.log(array1);
// expected output: Array [4, 5, 2, 3, 0, 1]
27
const array1 = [1, 2, 3]; const firstElement = array1.shift(); console. log(array1); console. log(firstElement);
// expected output: Array [2, 3] // expected output: 1
28
``` const array1 = ['one', 'two', 'three']; console.log('array1:', array1); ``` ``` const reversed = array1.reverse(); console.log('reversed:', reversed); ``` console.log('array1:', array1);
// expected output: "array1:" Array ["one", "two", "three"] // expected output: "reversed:" Array ["three", "two", "one"] // expected output: "array1:" Array ["three", "two", "one"]
29
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant']; console. log(animals.slice(2)); console. log(animals.slice(2, 4)); console. log(animals.slice(1, 5)); console. log(animals.slice(-2)); console. log(animals.slice(2, -1));
// expected output: Array ["camel", "duck", "elephant"] // expected output: Array ["camel", "duck"] // expected output: Array ["bison", "camel", "duck", "elephant"] // expected output: Array ["duck", "elephant"] // expected output: Array ["camel", "duck"]
30
const array = [1, 2, 3, 4, 5]; ``` // checks whether an element is even const even = (element) =\> element % 2 === 0; ``` console.log(array.some(even));
// expected output: true
31
const months = ['March', 'Jan', 'Feb', 'Dec']; months. sort(); console. log(months); const array1 = [1, 30, 4, 21, 100000]; array1. sort(); console. log(array1); const array2 = [10, 1, 30]; console. log(array1.sort((a,b) =\> a-b))) console. log(array1.sort((a,b) =\> b-a)))
// expected output: Array ["Dec", "Feb", "Jan", "March"] // expected output: Array [1, 100000, 21, 30, 4] // expected output: Array [1, 10, 30] // expected output: Array [30, 10, 1]
32
``` const months = ['Jan', 'March', 'April', 'June']; months.splice(1, 0, 'Feb'); ``` console. log(months); months. splice(4, 1, 'May'); console. log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "June"] ``` // replaces 1 element at index 4 // expected output: Array ["Jan", "Feb", "March", "April", "May"] ```
33
``` const array1 = [1, 'a', new Date('21 Dec 1997 14:12:00 UTC')]; const localeString = array1.toLocaleString('en', { timeZone: 'UTC' }); ``` console.log(localeString);
``` // expected output: "1,a,12/21/1997, 2:12:00 PM", // This assumes "en" locale and UTC timezone - your results may vary ```
34
const array1 = [1, 2, 'a', '1a']; console.log(array1.toString());
// expected output: "1,2,a,1a"
35
``` const array1 = ['a', 'b', 'c']; const iterator = array1.values(); ``` for (const value of iterator) { console.log(value); }
``` // expected output: "a" // expected output: "b" // expected output: "c" ```
36
const array1 = [1, 2, 3]; console. log(array1.unshift(4, 5)); console. log(array1);
// expected output: 5 // expected output: Array [4, 5, 1, 2, 3]