simple practice problems
for you to strengthen your understanding of map
, filter
, forEach
, and find
.
-
Given an array of numbers, create a new array where each number is doubled. (use map)
-
From an array of numbers, get only the even numbers. (use filter)
-
From an array of numbers, find the first number that is greater than 50. (use find)
-
Given an array of strings (names), print each name with
"Hello, "
in front. (use forEach) -
Given an array of numbers, create a new array of their squares. (use map)
-
From an array of ages, return only the ages that are 18 or older. (use filter)
-
From an array of numbers, find the first number that is divisible by 7. (use find)
-
From an array of words, print the length of each word. (use forEach)
-
Given an array of temperatures in Celsius, create a new array with the values converted to Fahrenheit. (use map)
-
From an array of products with prices, filter out only the products that cost more than 100. (use filter)
I’ll give you tasks + sample arrays when needed. You’ll figure out whether to use map
, filter
, forEach
, or find
yourself.
const users = [
{ id: 1, name: "Alice", age: 22 },
{ id: 2, name: "Bob", age: 17 },
{ id: 3, name: "Charlie", age: 25 }
];
Task: Get only the names from the users
array.
const products = [
{ id: 1, name: "Laptop", price: 1200 },
{ id: 2, name: "Phone", price: 800 },
{ id: 3, name: "Pen", price: 2 }
];
Task: Return only the products that cost less than 100.
const students = [
{ id: 1, name: "David", grade: "A" },
{ id: 2, name: "Eve", grade: "C" },
{ id: 3, name: "Frank", grade: "B" }
];
Task: Find the first student whose grade is "B"
.
const books = [
{ title: "Book One", pages: 200 },
{ title: "Book Two", pages: 150 },
{ title: "Book Three", pages: 300 }
];
Task: Print each book title with its page count.
const employees = [
{ id: 1, name: "Sam", salary: 5000 },
{ id: 2, name: "John", salary: 7000 },
{ id: 3, name: "Sara", salary: 4000 }
];
Task: Create a new array of employee names with their salaries increased by 10%.
const cars = [
{ brand: "Toyota", year: 2018 },
{ brand: "Honda", year: 2020 },
{ brand: "Ford", year: 2015 }
];
Task: Get only the cars manufactured after 2017.
const movies = [
{ id: 1, title: "Inception", rating: 8.8 },
{ id: 2, title: "The Room", rating: 3.6 },
{ id: 3, title: "Interstellar", rating: 8.6 }
];
Task: Find the first movie that has a rating below 5.
const players = [
{ name: "Messi", goals: 30 },
{ name: "Ronaldo", goals: 25 },
{ name: "Neymar", goals: 20 }
];
Task: Print each player's name with the number of goals they scored.
const courses = [
{ id: 1, title: "Math", completed: true },
{ id: 2, title: "English", completed: false },
{ id: 3, title: "Science", completed: true }
];
Task: Get only the titles of the courses that are completed.
const orders = [
{ id: 1, total: 250 },
{ id: 2, total: 400 },
{ id: 3, total: 150 }
];
Task: Create a new array containing only the total amounts from the orders.