101 Flashcards

1
Q

Describe the .forEach() , method

A

forEach() is an array method

It calls a provided callbackFn function once for each element in an array in ascending-index order. Unlike map(), forEach() always returns undefined

forEach((element) => { /* … / })
forEach((element, index) => { /
/ })
forEach((element, index, array) => { /
… */ })

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

Describe the .map() , method

A

map() is an array method

that returns a new array with each element being the result of the callback function

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

Explain the following code:

const kvArray = [
{ key: 1, value: 10 },
{ key: 2, value: 20 },
{ key: 3, value: 30 },
];

const reformattedArray = kvArray.map(({ key, value }) => ({ [key]: value }));

console.log(reformattedArray); // [{ 1: 10 }, { 2: 20 }, { 3: 30 }]
console.log(kvArray);
// [
// { key: 1, value: 10 },
// { key: 2, value: 20 },
// { key: 3, value: 30 }
// ]

A

the map() function is taking in destructured { key, value } as an argument

const reformattedArray = kvArray.map(({ key, value }) => ({ [key]: value }));

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

What does .split() do ?

A

split() is a String method that returns an array of strings based on a separator argument

const str = ‘The quick brown fox jumps over the lazy dog.’;

const words = str.split(‘ ‘);
console.log(words[3]);
// Expected output: “fox”

const chars = str.split(‘’);
console.log(chars[8]);
// Expected output: “k”

const strCopy = str.split();
console.log(strCopy);
// Expected output: Array [“The quick brown fox jumps over the lazy dog.”]

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

What is logged?

const str = ‘The quick brown fox jumps over the lazy dog.’;

const words = str.split(‘ ‘); <= space

console.log(words);

A

[“The”, “quick”, “brown”, “fox”, “jumps”, “over”, “the”, “lazy”, “dog.”]

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

What is logged?

const str = ‘quickly’;

const chars = str.split(‘ ‘); <= empty string, no space

console.log(chars);

A

[“q”, “u”, “i”, “c”, “k”, “l”, “y”]

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

What does join() do?

A

join() is an array method that creates and returns a new string by concatenating all of the elements in an array, separated by commas or a specified separator string.

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

console.log(elements.join());
// Expected output: “Fire,Air,Water” <= includes commas

console.log(elements.join(‘’)); <= empty string
// Expected output: “FireAirWater” <= no commas

console.log(elements.join(‘-‘));
// Expected output: “Fire-Air-Water”

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

What type of method is .join()

A

array method

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

what is the sort() method ?

A

array method

The sort() method sorts the elements of an array in place and returns the reference to the same array, now sorted

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

Rearrange the array into ascending order:

const array = [1, 30, 4, 21, 100000];

A

array.sort((a , b) => a - b);

console.log(array); <= mutates array!
// [1, 4, 21, 30, 100000]

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

Rearrange the array into descending order:

const array = [1, 30, 4, 21, 100000];

A

array.sort((a , b) => b - a);

console.log(array); <= mutates array!
// [100000, 30, 21, 4, 1]

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

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

How can you create a new list containing only words more than 6 characters long?

A

The filter() method creates a shallow copy of a portion of a given array, with elements that pass the test implemented by the provided function

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

console.log(result);
// Expected output: Array [“exuberant”, “destruction”, “present”]

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

what does the Array.includes() return ?

A

Boolean => true or false

the includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

includes(searchElement)

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

console.log(pets.includes(‘cat’));
// Expected output: true

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