methods: filter (), map (), reduce () Flashcards

1
Q

What does Array.filter do?

A

The filter() method creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.

it seems to loop through all elements of an array

e.g.,
const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];

const names = [
‘Ada’,
‘Hedy’,
‘Jean’,
‘Grace’,
‘Evelyn’,
‘Joan’,
‘Elizabeth’,
‘Janese’,
‘Donna’
];

const evenNumbers = numbers.filter(number => number % 2 === 0);
console.log(evenNumbers);

const overFive = numbers.filter(number => number > 5);
console.log(overFive);

const startWithE = names.filter(name => name[0] === ‘E’);
console.log(startWithE);

const haveD = names.filter(name => name.includes(‘d’) || name.includes(‘D’));
console.log(haveD);

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

What should the array.filter callback function return?

A

it should return conditionals

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

What is Array.filter useful for?

A

filtering with a method instead of filtering using loops

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

what does array.map( ) do?

A

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

e.g.

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];

const languages = [
‘Hypertext Markup Language’,
‘Cascading Style Sheets’,
‘ECMAScript’,
‘PHP Hypertext Preprocessor’,
‘Structured Query Language’
];

const doubled = numbers.map(x => x * 2);
console.log(doubled);

const prices = numbers.map(x => ‘$’ + x.toFixed(2));
console.log(prices);

const upperCased = languages.map(x => x.toUpperCase());
console.log(upperCased);

const firstLetters = languages.map(x => x[0]);
console.log(firstLetters);

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

What should the array.map callback function return?

A

a full, new, copied array, but with some operations performed on each element in the array

demo from mdn:

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

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

console.log(map1);
// Expected output: Array [2, 8, 18, 32]

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

What is Array.map useful for?

A

Useful for manipulating all elements in an array at the same time, so long as they all have the same operations performed on them

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

What does Array.reduce do?

A

Combining the elements of an array into a single value

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

What action should the callback function perform?

A

examine element, do computation on it, update accumulated value

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

What should the callback function return?

A

returns accumulated value

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

What is Array.reduce useful for?

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

What is a directory?

A

a folder!

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

What is a relative file path?

A

one on your system, which you can change, and it starts from the current working directory
-it never starts with a slash (but can be ‘./’ )

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

What is an absolute file path?

A

online, you cannot change, and it starts from
-it always starts with a slash

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

What module does Node.js include for manipulating the file system?

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

What method is available in the node:fs module for reading data from a file?

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

what is current directory syntax?

A

it is “.”

. means current direcltory

. /x means “look in current directory for file x