Reduce() Flashcards

(41 cards)

1
Q

What is the reduce() method used for in JavaScript?

A

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

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

How do you sum an array of numbers using reduce()?

A

numbers.reduce((acc, n) => acc + n, 0)

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

How do you multiply all numbers in an array using reduce()?

A

numbers.reduce((acc, n) => acc * n, 1)

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

How can you find the maximum value in an array using reduce()?

A

numbers.reduce((max, n) => n > max ? n : max, numbers[0])

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

How can you find the minimum value in an array using reduce()?

A

numbers.reduce((min, n) => n < min ? n : min, numbers[0])

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

How do you concatenate an array of strings using reduce()?

A

strings.reduce((acc, s) => acc + s, ‘’)

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

How can you count elements in an array using reduce()?

A

arr.reduce(count => count + 1, 0)

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

How do you convert an array of numbers to the sum of squares using reduce()?

A

numbers.reduce((acc, n) => acc + n * n, 0)

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

How can you create a comma-separated string from an array using reduce()?

A

arr.reduce((acc, x) => acc ? acc + ‘,’ + x : x, ‘’)

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

How do you reverse an array using reduce()?

A

arr.reduce((acc, x) => [x, …acc], [])

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

How can you remove duplicates from an array using reduce()?

A

arr.reduce((acc, x) => acc.includes(x) ? acc : […acc, x], [])

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

How do you count the frequency of elements in an array using reduce()?

A

arr.reduce((acc, x) => { acc[x] = (acc[x] || 0) + 1; return acc; }, {})

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

How can you group objects by a property using reduce()?

A

arr.reduce((acc, obj) => { (acc[obj.key] ||= []).push(obj); return acc; }, {})

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

How do you flatten a 2D array using reduce()?

A

arr.reduce((acc, x) => acc.concat(x), [])

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

How can you find the average of numbers in an array using reduce()?

A

arr.reduce((acc, n, _, { length }) => acc + n/length, 0)

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

How do you partition an array into even and odd numbers using reduce()?

A

arr.reduce((acc, n) => { acc[n%2?’odd’:’even’].push(n); return acc; }, {even:[], odd:[]})

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

How can you find unique characters in a string using reduce()?

A

str.split(‘’).reduce((acc, c) => acc.includes(c) ? acc : acc + c, ‘’)

18
Q

How do you merge an array of objects into one using reduce()?

A

arr.reduce((acc, obj) => ({…acc, …obj}), {})

19
Q

How can you count vowels in a string using reduce()?

A

str.split(‘’).reduce((acc, c) => ‘aeiou’.includes(c)?acc+1:acc, 0)

20
Q

How do you find the longest word in an array using reduce()?

A

arr.reduce((longest, w) => w.length > longest.length ? w : longest, ‘’)

21
Q

How can you check if all elements are positive using reduce()?

A

arr.reduce((acc, n) => acc && n > 0, true)

22
Q

How do you convert an array of key-value pairs to an object using reduce()?

A

arr.reduce((obj,[k,v]) => (obj[k]=v,obj),{})

23
Q

How can you implement map() using reduce()?

A

arr.reduce((acc, x) => […acc, x*2], [])

24
Q

How can you implement filter() using reduce()?

A

arr.reduce((acc, x) => x>0?[…acc,x]:acc, [])

25
How do you implement some() using reduce()?
arr.reduce((acc, x) => acc || x>0, false)
26
How can you implement every() using reduce()?
arr.reduce((acc, x) => acc && x>0, true)
27
How do you find the second largest number in an array using reduce()?
arr.reduce((acc, n) => {if(n>acc[0])return[n,acc[0]];if(n>acc[1])acc[1]=n;return acc;},[-Infinity,-Infinity])[1]
28
How can you create a histogram of word lengths using reduce()?
words.reduce((acc,w)=>(acc[w.length]=(acc[w.length]||0)+1,acc),{})
29
How do you compute running totals using reduce()?
arr.reduce((acc,x,i)=>(acc.push((acc[i-1]||0)+x),acc),[])
30
How can you zip two arrays using reduce()?
[...Array(arr1.length)].reduce((acc,_,i)=>acc.concat([[arr1[i],arr2[i]]]),[])
31
How do you shuffle an array using reduce()?
arr.reduce((acc,x,i)=>{const j=Math.floor(Math.random()*(i+1));[acc[i],acc[j]]=[acc[j],x];return acc;},[...arr])
32
How can you implement compose() function using reduceRight?
funcs.reduceRight((acc,f)=>(...args)=>f(acc(...args)),x=>x)
33
How do you deep flatten nested arrays using reduce()?
const flatten = arr => arr.reduce((acc,x)=>acc.concat(Array.isArray(x)?flatten(x):x),[])
34
How can you calculate the factorial of n using reduce()?
[...Array(n).keys()].map(i=>i+1).reduce((acc,x)=>acc*x,1)
35
How do you parse a query string into an object using reduce()?
str.split('&').reduce((acc,p)=>{let[k,v]=p.split('=');acc[k]=v;return acc;},{})
36
How can you find min and max values in a single pass using reduce()?
arr.reduce((acc,n)=>({min:Math.min(acc.min,n),max:Math.max(acc.max,n)}),{min:Infinity,max:-Infinity})
37
How do you chain promises sequentially using reduce()?
tasks.reduce((p,task)=>p.then(task),Promise.resolve())
38
How can you create a frequency map for top K frequent elements using reduce()?
arr.reduce((acc,x)=>{acc[x]=(acc[x]||0)+1;return acc;},{})
39
How do you convert a sentence into camelCase using reduce()?
sentence.split(' ').reduce((acc,word,i)=>acc+(i?word[0].toUpperCase()+word.slice(1):word.toLowerCase()),'')
40
How can you create a trie structure from an array of words using reduce()?
words.reduce((trie,word)=>{let node=trie;word.split('').forEach(c=>node=node[c]=node[c]||{});node.end=true;return trie;}, {})
41
What is a conceptual use of reduce() for custom debounce accumulator?
(Conceptual: combining inputs with reduce before emitting)