Loops & Iteration Flashcards

1
Q

When looping through arrays, why is a for…of loop preferred over a standard for loop?

A

A for…of will loop over the array once without the need for setting increments or conditionals

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

What is the full syntax of the forEach method?

A

The syntax of the forEach method, which is found on arrays:

arrayName.forEach((currentValue, index, array) ={

codeBlock of loop

})

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

Does the forEach method for arrays take a callback?

A

Yes, the forEach method takes a callback.

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

What are the three parameters for a forEach callback function?

A

currentValue, index, array

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

What is the most common way we would see a forEach method?

A

Looping over an array when we are concerned with the currentValue only.

arrayName.forEach((currentValue) => {doSomething with currentValue})

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

Which is faster, a forEach method call or a for…of loop?

A

The for…of loop is faster.

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

How can we refer to a variable name when looking at iteration syntax?

A

The variable name can also be called the “Identifier”

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

What is the returned value for a .forEach method call?

A

It is always undefined, we need to do something with the function product in the callback itself

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

How does the .forEach method work?

A

.forEach loops through the array and executes the callback function for each element. During each execution, the current element is passed as an argument to the callback function.

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

Are .forEach and for…of iteration methods equivilent?

A

No, .forEach does not work well with Async/Await and some edge cases. for…of iteration can make it difficult to get the index value and can’t be chained.

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

What is the difference between .map and .forEach?

A

.map creates a new array with the results of the callback function provided.

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

How is the .filter method used?

A

Use the .filter method to iterate over an array and then create a new array with elements that return true to the callback function

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

Does the .filter method create a new array?

A

Yes, it creates a new array out of elements that return true in the callback function.

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

What does the method .findIndex return?

A

It will return the index of the first element in an array that evaluates to true in the callback function.

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

What will .findIndex return if no elements evaluate to true in the callback function?

A

.findIndex will return -1 if no elements evaluate to true in the callback function.

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

What is the .reduce method used for in JavaScript?

A

The .reduce method allows us to simplify an input array based on the code block contained within the callback.

It is often used to sum an array but can be used to flatten an array (with concat).

17
Q

What is the syntax of a basic .reduce method call?

A

arrayName.reduce((accumulator, currentValue) => {

return accumulator + currentValue
}

18
Q

How do we set an initial value for a .reduce method call?

A

We pass in the initial value as the second parameter to the .reduce method, after the callback function.

19
Q

When must we supply an initial value to a .reduce method call?

A

We must supply an initial value to a .reduce method call when we are using reduce on an array of objects.

20
Q

What does the .map method do for us?

A

The .map method takes each value of an array, performs a callback function on the value, and creates a new array with that value.

21
Q

What is the typographical representation of the Rest or Spread operator?

A

( … ) is the typographical representation of the Rest or Spread operator