Given an array of numbers, create a new array that doubles each number, then filter out values less than 5, and finally calculate the sum of the remaining numbers. Then log the sum function in the console.
JavaScript arrays come with powerful built-in methods to help process and transform data more efficiently.
map()
creates a new array by applying a function to each element of the original array.
Example: Multiply each number by 2
const numbers = [1, 2, 3];
const doubled = numbers.map(num => num * 2);
console.log(doubled); // [2, 4, 6]
filter()
creates a new array with all elements that pass a test.
Example: Filter out numbers less than 3
const numbers = [1, 2, 3, 4];
const filtered = numbers.filter(num => num >= 3);
console.log(filtered); // [3, 4]
reduce()
reduces the array to a single value by applying a function that accumulates the result.
Example: Sum of numbers
const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((acc, curr) => acc + curr, 0);
console.log(sum); // 10