xxxxxxxxxx
// Certainly! The reduce() method in JavaScript is a powerful tool for iterating over an array and accumulating a single value based on the elements of that array. It takes a callback function as its argument and can be used in various ways. Here are 10 tricky questions that will help you understand and master the reduce() method:
const items = ['apple', 'banana', 'apple', 'cherry', 'banana', 'apple'];
const itemCount = items.reduce((accumulator, currentValue) => {
accumulator[currentValue] = (accumulator[currentValue] || 0) + 1;
return accumulator;
}, {});
console.log(itemCount); // Output: { apple: 3, banana: 2, cherry: 1 }
const nestedArray = [[1, 2], [3, 4], [5, 6]];
const flattenedArray = nestedArray.reduce((accumulator, currentValue) => accumulator.concat(currentValue), []);
console.log(flattenedArray); // Output: [1, 2, 3, 4, 5, 6]
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
console.log(sum); // Output: 15
const people = [
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 35 }
];
const totalAge = people.reduce((accumulator, person) => accumulator + person.age, 0);
console.log(totalAge); // Output: 90
const words = ['apple', 'banana', 'cherry', 'date', 'elderberry'];
const longestWord = words.reduce((longest, word) => (word.length > longest.length ? word : longest), '');
console.log(longestWord); // Output: 'elderberry'
const originalArray = [1, 2, 3, 4, 5];
const reversedArray = originalArray.reduce((accumulator, currentValue) => [currentValue].concat(accumulator), []);
console.log(reversedArray); // Output: [5, 4, 3, 2, 1]
const numbers = [4, 2, 9, 5, 1];
const maxNumber = numbers.reduce((max, number) => (number > max ? number : max), numbers[0]);
console.log(maxNumber); // Output: 9
const people = [
{ name: 'Alice', nationality: 'USA' },
{ name: 'Bob', nationality: 'Canada' },
{ name: 'Charlie', nationality: 'USA' }
];
const groupedByNationality = people.reduce((accumulator, person) => {
(accumulator[person.nationality] = accumulator[person.nationality] || []).push(person);
return accumulator;
}, {});
console.log(groupedByNationality);
/* Output:
{
USA: [
{ name: 'Alice', nationality: 'USA' },
{ name: 'Charlie', nationality: 'USA' }
],
Canada: [
{ name: 'Bob', nationality: 'Canada' }
]
}
*/
const functions = [
(x) => x * 2,
(x) => x + 3,
(x) => x - 1
];
const initialValue = 5;
const result = functions.reduce((acc, func) => func(acc), initialValue);
console.log(result); // Output: 11
xxxxxxxxxx
let arr = [1, 2, 3, 4, 5];
let newArr = arr.reduce((item1,item2) => {
return item1 + item2;
})
console.log(newArr)
// output 15
// 1+2+3+4+5
xxxxxxxxxx
// define a reusable function
const calculateSum = (arr) => {
return arr.reduce((total, current) => {
return total + current;
}, 0);
}
// try it
console.log(calculateSum([1, 2, 3, 4, 5]));
xxxxxxxxxx
const list = [1, 2, 3, 4, 5];
console.log(list.reduce((number, nextNumber) => number + nextNumber));
// --> 15
xxxxxxxxxx
/*
This method takes a callback that takes two parameters,
one that represents the element from the last iteration and the other is the current
element of the iteration
*/
let nums = [2,4,6,8,3,5]
let result = nums.reduce((prev,curr)=>prev+curr)
console.log(result); // 28
xxxxxxxxxx
let numbers = [1, 2, 3];
let sum = numbers.reduce(function (previousValue, currentValue) {
return previousValue + currentValue;
});
xxxxxxxxxx
const numbers = [260, 25, 35];
console.log(numbers.reduce(myFunc))
function myFunc(total, num) {
return total - num;
}
/*console.log will show 200 since the first element minus 35 and minus 25 is 260-25-35=200*/
/*while the name is "reduce", you do NOT have to subtract. Reduce() is more about "take the first element as total, and do something(whatever the function says) to it for each of the remaining elements; the remaining elements will be identified as the second parameter num for the function myFunc, you do whatever once for all remaining elements*/
xxxxxxxxxx
array.reduce(function(acumulador, elementoAtual, indexAtual, arrayOriginal), valorInicial)
xxxxxxxxxx
const numbers = [45, 4, 9, 16, 25];
let sum = numbers.reduce(myFunction);
function myFunction(total, value, index, array) {
return total + value;
}