L1- Array Methods Flashcards

1
Q

What does forEach return?

A

Undefined

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

Does .filter mutate?

A

no

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

Does .filter use a callback method?

A

yes. It examinds the return from each iteration for truthiness.

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

Does .map use a callback method?

A

Yes, whatever is return is push to a new array.

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

Can the array returned from .map be shorter than the caller?

A

no, it will be the same length even if elements have to be undefined.
EVEN IF the callback function changes the length of the original. But then it still creates empty elements and counts toward the length

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

Return true if at least 1 array element returns true from a callback function

SIMPLE LANGUAGE:
Do some of the array element follow this pattern?

A

myArray.some( )

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

What’s the difference between .some() and .includes()?

A

.includes() is just a simple comparison of the argument and array values.
.some() lets you use a callback function to test whatever logic you want.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
What is the difference between each of these (what do they do and return?)

**WHat do they return if they don't find the thing they're looking for?

Array.prototype.includes()
Array.prototype.some( )
Array.prototype.every()
Array.prototype.filter() **(no elements return truthy value)
Array.prototype.find() ** 
Array.prototype.indexOf() **
Array.prototype.findIndex() **
A

.includes() - returns boolean based on whether the argument is within the array

.some( ) - iterates over each element and returns boolean if at least 1 iteration returns true based on a callback function
.every() - iterates over each element and returns boolean if all iterations return true based on a callback function

.filter() - iterates over each element and returns an array based on whether the elements evaluate to true based on a callback function
**empty array

.find() - returns the value of the first element that returns true from a callback function
**Undefined

.indexOf() returns the first index of an element that matches the argument
**-1
Array.prototype.findIndex() returns the index of the first element that returns true from a callback function
**-1

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

What does
Array.prototype.some()
do?
and/or return?

A

“Are there some values in the array for which the given callback returns a truthy value?

tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn’t modify the array.

// Arrow function
some((element) => { /* ... */ } )
some((element, index) => { /* ... */ } )
some((element, index, array) => { /* ... */ } )

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

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

What does
Array.prototype.every()
do?
and/or return?

A

The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

// Arrow function
every((element) => { /* ... */ } )
every((element, index) => { /* ... */ } )
every((element, index, array) => { /* ... */ } )

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

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

Return true if all array elements returns true from a callback function

SIMPLE LANGUAGE:
Does every array element follow this pattern?

A

myArray.every()

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

Returns the first element that returns true from a callback function.

SIMPLE LANGUAGE:
What’s the first element in the array that follows this pattern?

A

myArray.find( callback fuction)

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

What does
Array.prototype.find()
do? and/or return?

A

The find() method returns the value of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.

// Arrow function
find((element) => { /* ... */ } )
find((element, index) => { /* ... */ } )
find((element, index, array) => { /* ... */ } )

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

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

Returns the index of the first element in the array that satisfies the provided testing/callback function. Otherwise, it returns -1, indicating that no element passed the test.

A

Array.prototype.findIndex()

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

What does:
Array.prototype.findIndex()
do and/or return?

A

Returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.

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

Reverse an array’s elements in place (mutating).

It also returns the reversed array

A

Array.prototype.reverse( )

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

What does
Array.prototype.reverse()
do?
and/or return?

A

reverses an array in place. The first array element becomes the last, and the last array element becomes the first.
Mutates

Returns the reversed array

reverse()

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

Reverese an array without mutating it

A

.slice().reverse()

19
Q

Return boolean if a string contains a specified substring
or
if an array element matches the entirety of the argument

SIMPLE LANGUAGE:
Does the array contain this entry?
Does the string contain this substring?

A

myStringOrArray.includes(‘value’)

20
Q

What’s the difference between .some() and .includes()?

A

.includes() is just a simple comparison of the argument and array values.
.some() lets you use a callback function to test whatever logic you want.

21
Q

What does
Array.prototype.includes()
do?
and/or return?

A

Determines whether an array includes a certain value among its entries, returning true or false as appropriate.

includes(searchElement)
includes(searchElement, fromIndex)

(There is also a String.prototype.includes())

22
Q

can .includes find NaN?

A

yes. it does not operate stricly on ===
But otherwise it’s the same.

23
Q

Replace an array’s elements between certain indexes with a SINGLE entry
Returns the changed array

SIMPLE LANGUAGE
Replace each consecutive elements with a 1 argument.

A

myArray.fill(replacement, start ind, end exclusive index)
mutates
[1, 2, 3, 4, 5, 6, 7, 8].fill(“apple”, 4, 6)
Expected output:
[ 1, 2, 3, 4, ‘apple’, ‘apple’, 7, 8 ]

24
Q

What does
Array.prototype.fill()
do?
and/or return?

A

Mutates and returns reference to array.

The fill() method changes all elements in an array to a static value, from a start index (default 0) to an end index (default array.length). It returns the modified array.

Mutates
Returns reference to original array

fill(value)
fill(value, start)
fill(value, start, end)

25
Q

What will this do: arr.fill(1, 1, 3);

A

replaces elements with first arg
Starts at second arg
Ends just before third arg

26
Q

What is returne from .map?
[‘ant’, ‘bear’].map(elem => {
if (elem.length > 3) {
return elem;
}
});

A

[ undefined, ‘bear’ ]

27
Q

What is returns from this?

[‘ant’, ‘bear’, ‘caterpillar’].pop().length;

A

11

28
Q

Write code to output something like this:
flintstonesObj; // { Fred: 0, Barney: 1, Wilma: 2, Betty: 3, Pebbles: 4, Bambam: 5 }

From
let flintstones = [“Fred”, “Barney”, “Wilma”, “Betty”, “Pebbles”, “Bambam”];

(you shouldn’t need Object.entries)

A

let flintstonesObj = {};

flintstones.forEach((name, index) => {
flintstonesObj[name] = index;
});

flintstonesObj; // { Fred: 0, Barney: 1, Wilma: 2, Betty: 3, Pebbles: 4, Bambam: 5 }

29
Q

Write code to get the sum of ages.
let ages = {
Herman: 32,
Lily: 30,
Grandpa: 5843,
Eddie: 10,
Marilyn: 22,
Spot: 237
};

A

let totalAges = 0;
Object.values(ages).forEach(age => totalAges += age);
totalAges; // => 6174

30
Q

Pick out the minimum age from our current Munster family object: (2 ways)

let ages = {
Herman: 32,
Lily: 30,
Grandpa: 5843,
Eddie: 10,
Marilyn: 22,
Spot: 237
};

A

let smallest = Object.values(ages).sort((a, b) => a - b)[0]
or
let agesArr = Object.values(ages);
Math.min(…agesArr); // => 10

31
Q

How do you input all of an arrays elements as arguments into a function?

A

…[array]

32
Q

Create an object that expresses the frequency with which each letter occurs in this string: (2 ways)
‘The apple orchard blah blah blah’
ex:
{ T: 1, h: 1, e: 2, F: 1, l: 1, … }

A

charsInStatement.forEach(char => {
result[char] = result[char] || 0;
result[char] += 1;
});

33
Q
A

D

34
Q
A

B

35
Q

What does .sort() return/mutate?

A

It mutates
Sorted alphabetically (return b > a)

36
Q

What does this return: console.log([‘asdf’].includes(‘asd’))

A

false

37
Q

What’s the difference between .indexOf() and .findIndex()

A

indexof is just looking for strict equality
find index is looking for the first element that returns true from a call back function

38
Q

Remove element from beginning, return that element

A

.shift

39
Q

Create new array, with the caller and argument(s) together in one array

A

.concat

40
Q

does .fill mutaet?

A

yes

41
Q

Does sort or reverse mutate?

A

both mutate

42
Q
A
43
Q

What happens when you call .reduce on an empty array and an initial accumulator value of 0?

On an empty array with with no initial accumulator value

On an array with 1 element and no initial accumulator value

On an array with 2 or more elements and no initial accumulator value

A

returns 0

Type error

Whatever the value of that one element is
Because there is no previous iteration and no initial accumulator value, .reduce uses the first element as the return from the first iteration for some reason.

Same. Can have unexpected results. Always have an initial value.