Iterators Flashcards

(13 cards)

1
Q

Describe what Iterators are in JavaScript?

A

Iterators are in-built array methods called on arrays to manipulate elements and return array values.

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

Name 3 Iterator methods

A

.forEach()
.map()
.filter()

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

How is the .forEach() iterator written and what does it do?

A

Goes through each element in an array.

const cars = [civic, jazz, corsa];

cars.forEach((car) => {
console.log(car);
})

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

What is the return value for the .forEach() iterator\array method.

A

Undefined

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

How is the .map() iterator written and what does it do?

A

Goes through each element and manipulates the data and then returns a new value.

const numbers = [1, 2, 3];

const largenumbers = numbers.map(number => {
return number * 100;
})

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

What does the .join do?

A

Joins all elements in an array, the brackets holds the seperator. If omitted a coma is added by default.

console.log(secretMessage.join(‘’));

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

How is the .filter() iterator written and what does it do?

A

The filter iterator allows you to filer array elements from the original array. The new array then contains the filtered elements only (Only values that are true will be included in the new array).

const cars = [BNW, Honda, Jeep, Ford];
const cars1 = cars.filter(car => {
return car.length > 3;
});
console.log(cars1);

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

How is the .findIndex() iterator written and what does it do?

A

Finds the first array element that evaluates to true and provides the index number of the element. If the array contains elements that don’t evaluate to true then the method returns a value of -1.

const animals = [‘cheetah’, ‘monkey’, ‘salamander’, ‘elephant’];
const foundAnimal = animals.findIndex(animal => {
return animal === ‘elephant’;
})
console.log(foundAnimal);

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

What does the .reduce() iterator do?

A

The reduce method reduces all elements in an array to a single value. The method takes two arguments(accumulator, currentValue), the first two elements of an array and adds them together to become the new accumulator value.

After the initial loop, only a single element from an array is taken and to the new accumulator value.

ALSO the iterator can take a second optional parameter which becomes the first argument value (Accumulator).

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

How is the .reduce() iterator written?

A

const newNumbers = [1, 3, 5, 7];
const newSum = newNumbers.reduce((accumulator, currentValue) => {
return accumulator + currentValue;
}, 10);

The code above has a second argument (Placed after the code block that has a value of 10. This means accumulator is 10 and currentValue is 1.

else it would be
accumulator is 1 and currentValue is 3.

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

Explain .some()

A

returns true or false, at least a single element needs to match the condition for true to be returned.

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

Explain .every()

A

returns true or false, all elements in the array need to match the condition for true or else it’s false.

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