Iterators Flashcards

1
Q

There are three important things to know about the .forEach() method.

A

It is an array method. It must be called upon an array.
Any changes to the iterated array value won’t be updated in the original array.
The return value is undefined.

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

Review: Iterators

A

.forEach() is used to execute the same code on every element in an array but does not change the array and returns undefined.
.map() executes the same code on every element in an array and returns a new array with the updated elements.
.filter() checks every element in an array to see if it meets certain criteria and returns a new array with the elements that return truthy for the criteria.
All iterator methods can be written using arrow function syntax. In fact, given the succinctness and the implicit return of arrow function syntax, this is quickly becoming the preferred way to write these types of method calls.
You can visit the Mozilla Developer Network to learn more about iterator methods (and all other parts of JavaScript!).
Additional iterator methods such as .some(), .every(), .reduce() perform different functions.

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

.map()

A

Returns new array with contents changed per code block

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

.filter()

A

.filter() returns certain elements from the original array that evaluate to truthy based on conditions written in the block of the method.

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

split()

A

The split() method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.

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

Which of the following methods returns a boolean value?

Which of the following methods returns a boolean value?

.some()

.filter()

.map()

.forEach()

A

.some()

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

What will the following code return?

let animals = [‘bears’, ‘cats’, ‘dogs’, ‘elephants’, ‘giraffes’];

animals.some(animal => animal.length < 5);
What will the following code return?

[‘cats’, ‘dogs’]

true

false

A

true

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

What is the correct way to refactor the code below to use arrow function syntax?

namesArray.forEach(function(name) {
console.log(‘Welcome, ‘ + name + ‘!’);
}
What is the correct way to refactor the code below to use arrow function syntax?

namesArray.forEach => console.log(‘Welcome, ‘ + name + ‘!’);

namesArray.forEach( => console.log(‘Welcome, ‘ + name + ‘!’));

namesArray.forEach(function => name {
console.log(‘Welcome, ‘ + name + ‘!’);
}

namesArray.forEach(name => console.log(‘Welcome, ‘ + name + ‘!’));

A

namesArray.forEach(name => console.log(‘Welcome, ‘ + name + ‘!’));

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

Which of the following iterator methods returns undefined?

Which of the following iterator methods returns undefined?

.map()

.filter()

.forEach()

.some()

A

.forEach()

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

Which of the following methods returns a new array?

Which of the following methods returns a new array?

.some()

.forEach()

.every()

.filter();

A

.filter();

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

Which of the following methods returns an array with values that evaluate to truthy based on the condition in the method’s block?

Which of the following methods returns an array with values that evaluate to truthy based on the condition in the method’s block?

.forEach()

.some()

.map()

.filter()

A

.filter()

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