EXERCIESES FOR THE .map, .filter, .reduce functions

Esercizio 1

Task: You have an array of numbers, and you need to create a new array where each number is squared.

Input: [1, 2, 3, 4, 5]
Output: [1, 4, 9, 16, 25]
const array_originale = [1, 2, 3, 4, 5];
const quadrato_numeri = array_originale.map(Element => Element * Element);
    console.log(quadrato_numeri);
                

Esercizio 2

Task: You have an array of numbers, and you need to create a new array that only contains the even numbers.

Input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Output: [2, 4, 6, 8, 10]
const numeri = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const numeri_pari = numeri.filter(num => num % 2 === 0);
    console.log(numeri_pari);
                

Esercizio 3

Task: You have an array of strings, and you need to create a new array where each string is capitalized.

Input: ["apple", "banana", "cherry"]
Output: ["APPLE", "BANANA", "CHERRY"]
const fruits = ["apple", "banana", "cherry"];
const capitalizedFruits = fruits.map(fruit => fruit.toUpperCase());
console.log(capitalizedFruits); // Output: ["APPLE", "BANANA", "CHERRY"]                    
                

Esercizio 4

Task: You have an array of objects representing people, and you need to create a new array that only contains people who are older than 18.

Input: const people = [
    { name: "Alice", age: 17 },
    { name: "Bob", age: 19 },
    { name: "Charlie", age: 16 },
    { name: "David", age: 22 }
];
                
Output:   [
    { name: "Bob", age: 19 },
    { name: "David", age: 22 }
]

                
const people = [
    { name: "Alice", age: 17 },
    { name: "Bob", age: 19 },
    { name: "Charlie", age: 16 },
    { name: "David", age: 22 }
];
const adults = people.filter(person => person.age > 18);
console.log(adults); // Output: [{ name: "Bob", age: 19 }, { name: "David", age: 22 }]

                

Esercizio 5

Task: You have an array of objects representing products with their prices in cents. Create a new array of objects where the prices are converted to dollars.

INPUT const products = [
    { name: "Shirt", price: 1999 },
    { name: "Pants", price: 2999 },
    { name: "Socks", price: 499 }
];
                

OUTPUT  [
            { name: "Shirt", price: 19.99 },
            { name: "Pants", price: 29.99 },
            { name: "Socks", price: 4.99 }
    ]
               
    const products = [
    { name: "Shirt", price: 1999 },
    { name: "Pants", price: 2999 },
    { name: "Socks", price: 499 }
];
const productsInDollars = products.map(product => ({
    name: product.name,
    price: product.price / 100
}));
console.log(productsInDollars);
// Output: [{ name: "Shirt", price: 19.99 }, { name: "Pants", price: 29.99 }, { name: "Socks", price: 4.99 }]
                                 
                

Esercizio 6

Task: You have an array of strings, and you need to create a new array that only contains the strings with more than 5 characters.

Input: ["hello", "world", "JavaScript", "is", "awesome"]
Output: ["JavaScript", "awesome"]
const words = ["hello", "world", "JavaScript", "is", "awesome"];
const longWords = words.filter(word => word.length > 5);
    console.log(longWords); // Output: ["JavaScript", "awesome"]

            

FILTER + MAP

Esercizio 7

Task: You have an array of numbers. First, filter out the even numbers, and then create a new array where each of those even numbers is squared.

Input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Output: [4, 16, 36, 64, 100]
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const evenSquares = numbers
    .filter(num => num % 2 === 0)
    .map(num => num * num);
console.log(evenSquares); // Output: [4, 16, 36, 64, 100]
                

            

Esercizio 8

Task: You have an array of strings. Filter out the strings that have more than 3 characters, and then capitalize the remaining strings.

Input: ["hi", "hello", "hey", "yo", "JavaScript"]
Output: ["HELLO", "HEY", "JAVASCRIPT"]
const words = ["hi", "hello", "hey", "yo", "JavaScript"];
const longWordsCapitalized = words
    .filter(word => word.length > 3)
    .map(word => word.toUpperCase());
console.log(longWordsCapitalized); // Output: ["HELLO", "HEY", "JAVASCRIPT"]
                
                

            

Esercizio 9

Task: You have an array of objects representing people. Filter out those who are 18 years old or younger, and then create an array of just their names.

INPUT:   const people = [
        { name: "Alice", age: 17 },
        { name: "Bob", age: 19 },
        { name: "Charlie", age: 16 },
        { name: "David", age: 22 }
    ];
            

Output: ["Bob", "David"]
    const people = [
    { name: "Alice", age: 17 },
    { name: "Bob", age: 19 },
    { name: "Charlie", age: 16 },
    { name: "David", age: 22 }
];
const adultNames = people
    .filter(person => person.age > 18)
    .map(person => person.name);
console.log(adultNames); // Output: ["Bob", "David"]
            

Esercizio 10

Task: You have an array of products with their prices in cents. Filter out the products that cost more than $20, and then format the remaining products' prices into a string with a dollar sign.

INPUT:       const products = [
                { name: "Shirt", price: 1999 },
                { name: "Pants", price: 2999 },
                { name: "Socks", price: 499 }
            ];
            
            

OUTPUT                [
    { name: "Shirt", price: "$19.99" },
    { name: "Socks", price: "$4.99" }
]
            
                const products = [
                { name: "Shirt", price: 1999 },
                { name: "Pants", price: 2999 },
                { name: "Socks", price: 499 }
            ];
            const affordableProducts = products
                .filter(product => product.price <= 2000)
                .map(product => ({
                    name: product.name,
                    price: `$${(product.price / 100).toFixed(2)}`
                }));
            console.log(affordableProducts);
            // Output: [{ name: "Shirt", price: "$19.99" }, { name: "Socks", price: "$4.99" }]
            
            

Esercizio 11

Task: You have an array of numbers, including negative numbers. Filter out the negative numbers, and then create a new array that contains the square roots of the remaining positive numbers.

Input: [-4, 16, -1, 25, 9, -9, 4]
Output: [4, 5, 3, 2]
                const numbers = [-4, 16, -1, 25, 9, -9, 4];
                const positiveSquareRoots = numbers
                    .filter(num => num >= 0)
                    .map(num => Math.sqrt(num));
                console.log(positiveSquareRoots); // Output: [4, 5, 3, 2]                
            

Exercises Using .reduce

Esercizio 12

Task: You have an array of numbers. Use .reduce to calculate the sum of all the numbers.

Input: [1, 2, 3, 4, 5]
Output: 15
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((acc, curr) => acc+curr, 0);
    console.log(sum);
            
            

Esercizio 13

Task: You have an array of numbers. Use .reduce to calculate the product of all the numbers.

Input: [2, 3, 4]
Output: 24
const numbers = [2, 3, 4];
const product = numbers.reduce((acc, curr) => acc*curr);
    console.log(product);
            
            

Esercizio 14

Task: You have an array of strings. Use .reduce to concatenate them all into a single string.

Input: ["Hello", " ", "world", "!"]
Output: "Hello world!"
        const words = ["Hello", " ", "world", "!"];

        const phrase = words.reduce((acc, curr) => acc+curr);
            console.log(phrase);    
            

Esercizio 15

Task: You have an array of numbers. Use .reduce to find the maximum number.

Input: [5, 1, 8, 3, 7]
Output: 8
    const numbers = [5, 1, 8, 3, 7];

    const max = numbers.reduce((max, num) => num > max ? num : max, numbers[0]);
        // condition ? exprIfTrue : exprIfFalse
        console.log(max);
            

Esercizio 16

Task: You have an array of strings and a target string. Use .reduce to count how many times the target string appears in the array.

Input: ["apple", "banana", "apple", "orange", "apple"], target: "apple"
Output: 3
const fruits = ["apple", "banana", "apple", "orange", "apple"];

const target = "apple";
const occurencies = fruits.reduce((acc, fruit) => fruit === target ? acc + 1 : acc, 0);

    console.log(occurencies);
            

Exercises Using .map, .filter, and .reduce

Esercizio 17

Task: You have an array of products with their prices. First, filter out products that cost more than $20. Then, create a new array with just the prices. Finally, use .reduce to calculate the total cost of the remaining products.

const products = [
    { name: "Shirt", price: 1999 },
    { name: "Pants", price: 2999 },
    { name: "Socks", price: 499 }
];
            

Output: 24.98
    const products = [
    { name: "Shirt", price: 1999 },
    { name: "Pants", price: 2999 },
    { name: "Socks", price: 499 }
];
    
const new_products = products
            .filter(product => product.price < 2000)
            .map(product => product.price /100)
            .reduce((acc, price) => acc+price, 0);

            console.log(new_products);
            

Esercizio 18

Task: You have an array of people with their ages. First, filter out those younger than 18. Then, map the remaining ages to a new array. Finally, use .reduce to calculate the average age of the remaining people.

    const people = [
    { name: "Alice", age: 17 },
    { name: "Bob", age: 25 },
    { name: "Charlie", age: 16 },
    { name: "David", age: 22 }
];
            

Output: 23.5
    const people = [
    { name: "Alice", age: 17 },
    { name: "Bob", age: 25 },
    { name: "Charlie", age: 16 },
    { name: "David", age: 22 }
];

const newGroup = people
                .filter(people => people.age >= 18) //attento all sotto.proprietà ! .age .name etc...
                .map(person => person.age)
                .reduce((acc, age, _, arr) => acc+age / arr.length, 0);
console.log(newGroup);
            

Esercizio 19

Task: You have an array of words. First, filter out words shorter than 4 characters. Then, map the remaining words to their lengths. Finally, use .reduce to calculate the total length of these words.

Input: ["hello", "world", "JS", "is", "awesome"]
Output: 17
const words = ["hello", "world", "JS", "is", "awesome"];

const words_lenght = words
                    .filter(word => word.length >= 4)
                    .map(word => word.length)
                    .reduce((acc, word) => acc+word, 0);
console.log(words_lenght);
            

Esercizio 20

Task: You have an array of students with their grades. First, filter out students who scored below 50. Then, map the remaining grades to a percentage (out of 100). Finally, use .reduce to calculate the average grade of the remaining students.

const students = [
    { name: "Alice", grade: 48 },
    { name: "Bob", grade: 78 },
    { name: "Charlie", grade: 92 },
    { name: "David", grade: 62 }
];
            

Output: 77.33
    const students = [
    { name: "Alice", grade: 48 },
    { name: "Bob", grade: 78 },
    { name: "Charlie", grade: 92 },
    { name: "David", grade: 62 }
];

const students_Filtered = students
                        .filter(student => student.grade >= 50)
                        .map(student => student.grade)
                        .reduce((acc, grade, _, arr) => acc+grade / arr.length, 0);
console.log(students_Filtered);
            

Esercizio 21

Task: You have an array of employees with their salaries. First, filter out e mployees who earn less than $50,000. Then, map the remaining salaries to thousands (e.g., $60,000 → 60). Finally, use .reduce to calculate the total salary in thousands of the remaining employees.

                const employees = [
                { name: "Alice", salary: 45000 },
                { name: "Bob", salary: 75000 },
                { name: "Charlie", salary: 120000 },
                { name: "David", salary: 50000 }
            ];
            
            

Output: 245
    const employees = [
    { name: "Alice", salary: 45000 },
    { name: "Bob", salary: 75000 },
    { name: "Charlie", salary: 120000 },
    { name: "David", salary: 50000 }
];


const new_employee_list = employees
                            .filter(employee => employee.salary >= 50000)
                            .map(employee => employee.salary/1000 )
                            .reduce((acc, salary) => acc+salary, 0);
console.log(new_employee_list + "$");