Array.Reduce() Flashcards Preview

REVIEW: Javascript S.A. Study Guide > Array.Reduce() > Flashcards

Flashcards in Array.Reduce() Deck (44)
Loading flashcards...
1
Q
const array1 = [1, 2, 3, 4];
const reducer = 
console.log(array1.reduce(reducer));
// 1 + 2 + 3 + 4
A

(accumulator, currentValue) => accumulator + currentValue;

2
Q
const array1 = [1, 2, 3, 4];
const reducer = 

console.log(array1.reduce(reducer, 5));

A

const reducer = (accumulator, currentValue) => accumulator + currentValue;

3
Q

The reducer function takes four arguments:

1.
2.
3.
4.

A
Accumulator (acc)
Current Value (cur)
Current Index (idx)
Source Array (src)
4
Q

arr.reduce(_____(_____, _______[, index[, array]]), [, initialValue])

A

callback Function
accumulator
Current Value

5
Q

The __________ adds the callback’s return values. It is the added value previously returned in the last invocation of the callback, or initialValue, if supplied (see below)

A

accumulator

6
Q

The current element being processed in the array.

A

currentValue

7
Q

the 3 optional values are:

A

currentIndex
array
initialValue

8
Q

The current element being processed in the array.

A

currentValue

9
Q

The index of the current element being processed in the array. Starts from index 0 if an initialValue is provided. Otherwise, starts from index 1.

A

currentIndex

10
Q

A value to use as the first argument to the first call of the callback. If no initialValue is supplied, the first element in the array will be used. Calling reduce() on an empty array without an initialValue will throw a TypeError.

A

initialValue

11
Q

The first time the callback is called, _________ and __________ can be one of two values.

A

accumulator

currentValue

12
Q

If initialValue is not provided, reduce() will execute the callback function starting at __________ skipping the first index. If initialValue is provided, it will start at _______

A

index 1

index 0

13
Q
var sum = [0, 1, 2, 3].reduce( 
// code here
)
// sum is 6
A
function (accumulator, currentValue) {
  return accumulator + currentValue;
}
14
Q
var total = [ 0, 1, 2, 3 ].reduce(
// code here
);
A

( accumulator, currentValue ) => accumulator + currentValue

15
Q

To sum up the values contained in an array of objects, you must supply an_________, so that each item passes through your function.

A

initialValue

16
Q
var sum = [{x: 1}, {x: 2}, {x: 3}].reduce(function (accumulator, currentValue) {
    return \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
},0)

console.log(sum) // logs 6

A

accumulator + currentValue.x;

17
Q
var initialValue = 0;
var sum = [{x: 1}, {x: 2}, {x: 3}].reduce(function (accumulator, currentValue) {
    return accumulator + currentValue.x;
},initialValue)

WRITE AS A ARROW FUNCTION

A

( (accumulator, currentValue) => accumulator + currentValue.x
,initialValue
);

18
Q

var flattened = [[0, 1], [2, 3], [4, 5]].reduce(accumulator, currentValue =>

);
// flattened is [0, 1, 2, 3, 4, 5]
A

accumulator.concat(currentValue);

19
Q

If you are using an environment compatible with Set and Array.from(), you could use

let orderedArray = // ->?

to get an array where duplicate items have been removed.

A

Array.from(new Set(myArray));

20
Q

const array1 = [[0, 1], [2, 3], [4, 5]].____________(
(accumulator, currentValue) => accumulator.concat(currentValue)
);

console.log(array1);
// expected output: Array [4, 5, 2, 3, 0, 1]
A

reduceRight

21
Q

arr.reduce(callback(accumulator, currentValue[, _______[,______]]), [,________])

A

index
array
initial value

22
Q

you use intiliaze a object with reduce by adding

A

comma with braces to the end of the function

23
Q

what’s is this missing

var flattened = [[0, 1], [2, 3], [4, 5]].reduce(
  function(accumulator, currentValue) {
    return accumulator.concat(currentValue);
  },

);

A

array brackets at end

24
Q

Calling reduce() on an empty array without an initialValue will __________

A

throw a TypeError.

25
Q

[ ].reduce( maxCallback ); //

A

TypeError

26
Q

_______ will increment the value of i, and then return the incremented value.

A

++i

27
Q

________ will increment the value of i, but return the original value that i held before being incremented.

A

i++

28
Q

i = 1;
j = ++i;
(i is ___, j is _____)

A

2

2

29
Q

i = 1;
j = i++;
(i is ____, j is _____)

A

2

1

30
Q
const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
console.log(array1.reduce(reducer));
// expected output:
A

10

31
Q
const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
// 1 + 2 + 3 + 4
console.log(\_\_\_\_\_\_\_\_\_\_\_\_\_);
// To get expected output of 10
A

array1.reduce(reducer)

32
Q

Sum all the values of an array using arrow

var sum = [0, 1, 2, 3]

// sum is 6

A

reduce(
( accumulator, currentValue ) => accumulator + currentValue,
0 );

33
Q

Sum all the values of an array using function

var sum = [0, 1, 2, 3]

// sum is 6

A

.reduce(function (accumulator, currentValue) {
return accumulator + currentValue;
}, 0);

34
Q

const array1 = [[0, 1], [2, 3], [4, 5]].reduceRight(

CODE HERE

console.log(array1);
// expected output: Array [5, 4, 2, 3, 0, 1]
A

(accumulator, currentValue) => accumulator.concat(currentValue)
);

35
Q

The _____________ method applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value.

A

reduceRight()

36
Q

The __________ method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.

A

reducer

37
Q

let arr = [1, 2, 3, 4, 5];

add all numbers in array
let result =

alert(result); // 15

A

arr.reduce((sum, current) => sum + current, 0);

38
Q
let value = arr.reduce(function(\_\_\_,\_\_\_,\_\_\_\_\_,\_\_\_\_\_\_) {
  // ...
}, initial);
A

previousValue, item, index, array

39
Q

WHAT CAN BE REMOVED AND WHY

let arr = [1, 2, 3, 4, 5];

let result = arr.reduce((sum, current) => sum + current, 0);

alert(result); // 15

A

0

current value at 0

40
Q
const numbers = [5, 10, 15];
const total = numbers.reduce( (accumulator, currentValue) => accumulator + currentValue, 44);

console.log(total);

A

74

30 from function + the 44 start point

41
Q
var a = ['1', '2', '3', '4', '5']; 
var left  = a.reduce

console.log(left); // “12345”

A

(function(prev, cur) { return prev + cur; });

42
Q

var a = [‘1’, ‘2’, ‘3’, ‘4’, ‘5’];

var right = a.reduceRight

console.log(right); // “54321”

A

(function(prev, cur) { return prev + cur; });

43
Q

the _____ doesn’t have to be used to accumulate a number rather it can be used to return the higest price.

A

accumulator

44
Q
const products = [
  { name: 'hard drive', price: 59.99 },
  { name: 'lighbulbs', price: 2.59 },
  { name: 'paper towels', price: 6.99 },
  { name: 'flatscreen monitor', price: 159.99 },
  { name: 'cable ties', price: 19.99 },
  { name: 'ballpoint pens', price: 4.49 }
];
A
const product = products
  .filter(product => product.price < 10)
  .reduce((highest, product) => {
    if (highest.price > product.price) {
      return highest;
    }
    return product;
  }, { price: 0 });