JavaScript focused study deck Flashcards

1
Q

What does a for…in loop return to us?

A

A for…in loop returns the keys of an object.

In the case of an Array, this would be the indices

This also works for strings

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

What does a for…of loop return to us?

A

A for…of loop returns each element in the iterable.

This works for strings and arrays, but not objects

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

What does the filter method return to us?

A

The filter method returns a new array with elements that match the given conditionals (the given test implemented by the given function)

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

What argument must we provide to a filter method?

A

We must provide a Callback function that runs the filtering test.

This can be a function expression, an anonymous function, or an arrow function.

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

Beside a callback function, what arguments can we pass to the filter method?

A

We can pass the index, and the array on which to run the callback

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

What does the map method return to us?

A

The map method returns a new array populated with the results of calling a provided function on each element of the original array.

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

What argument must we pass to the map method?

A

We must pass a callback function to the map method.

This callback can be a function expression, an anonymous function, or an arrow function.

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

What arguments, besides a callback, with the map method take?

A

The map method will take index as an argument, as well as the array on which to run the callback.

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

When shouldn’t we use the map function?

A

We shouldn’t use the map function when we don’t need the returned array.

In that case you should use a for…of loop or forEach

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

Sometimes a method might return an array with undefined, NaN, or ‘ ’ at either end based on our callback.

What can we do about that?

A

We can splice the returned array to remove the index value we do not want.

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

Does the splice method modify the array it is performed on?

A

Yes, it modifies the original array.

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

What arguments does splice take in?

A

The splice method takes in a starting index, a delete count, and then items to be inserted.

Delete count is optional, but must be included if there are items to be inserted

Delete count can be 0

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

Does the slice method alter the array it is called upon?

A

No, the slice method returns a shallow copy of the array in a new array

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

What arguments does slice take?

A

Slice takes the starting index (inclusive), and the end index (exclusive)

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

Does the sort method return a new array?

A

No, sort will sort the items in place, modifying the original array.

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

What is the default sorting order of the sort method?

17
Q

What arguments do we pass to the sort method?

A

We pass either 0 arguments for the default sort, or a callback function (expression, anonymous, or arrow).

18
Q

When comparing elements in a sort method, what determines their placement in relation to one another?

A

The return value of the compare function determines their sort order.

>0 b before a

<0 a before b

===0 keep original order

19
Q

How can we sort alphabetically with unique characters in our string or array?

A

We can use the localeCompare

a.localeCompare(b) to sort them in ascending order

20
Q

Can we use dot notation in our sort method callback function?

A

Yes, we can use dot notation to sort values in an array of object.

21
Q

Can we put conditionals in our sort method callback function?

A

Yes, we can put conditionals in our sort method callback, we need to account for return values of -1, 0, and 1

22
Q

What does the reduce method return to us?

A

A single value

23
Q

Does reduce modify the array?

A

No, not in its typical use, reduce does not modify the array.

24
Q

What arguments does reduce accept?

A

Reduce accepts an a callback function with an accumulator and a current value argument, as well as an optional initial value.

25
Where does the initial value argument go in a reduce method call?
The initial value goes after the callback function, separated by a comma
26
What arguments does the callback function passed to the reduce method accept?
The reduce method callback function accepts an accumulator, a current value, current index, and array arguments.
27
What is a benefit of using reduce on a multi-tier array?
The reduce function can be used to flatten an array
28
How do we flatten an array with the reduce method?
We have the returned value of the callback function = previousvalue.concat(currentvalue)
29
Can we use the reduce method to return a count of elements in an array?
Yes, we can use reduce to add names to an object that holds the elements as keys and increment a counter as the value if(name in AllNames) {allNames[name]++} else {allNames[name]=1} return allNames