๐ Scenario: You are working on a small online store. You have a list of products with their prices. You want to find only the products that cost less than $20 to show a special discount section.
๐ฏ Goal: Build a program that filters the list of products to keep only those with a price less than $20 using the filter method.
๐ What You'll Learn
Create an array called products with objects containing name and price properties.
Create a variable called maxPrice and set it to 20.
Use the filter method on products to create a new array called affordableProducts with products priced less than maxPrice.
Print the affordableProducts array to the console.
๐ก Why This Matters
๐ Real World
Filtering lists of items is common in online stores, apps, and data processing to show only relevant information to users.
๐ผ Career
Knowing how to use the filter method helps you work with data arrays efficiently, a key skill for frontend and backend developers.
Progress0 / 4 steps
1
Create the products array
Create an array called products with these exact objects: { name: 'Pen', price: 5 }, { name: 'Notebook', price: 15 }, { name: 'Backpack', price: 45 }, { name: 'Pencil', price: 3 }, { name: 'Calculator', price: 25 }.
Javascript
Hint
Use square brackets [] to create an array. Each product is an object with name and price.
2
Set the maximum price
Create a variable called maxPrice and set it to the number 20.
Javascript
Hint
Use const maxPrice = 20; to create the variable.
3
Filter affordable products
Use the filter method on products to create a new array called affordableProducts that contains only products with a price less than maxPrice.
Javascript
Hint
Use products.filter(product => product.price < maxPrice) to get the filtered array.
4
Print the filtered products
Print the affordableProducts array to the console using console.log.
Javascript
Hint
Use console.log(affordableProducts); to print the filtered list.
Practice
(1/5)
1. What does the JavaScript filter method do?
easy
A. Sorts the array in ascending order
B. Creates a new array with elements that pass a test
C. Changes the original array by removing elements
D. Combines all elements into a single value
Solution
Step 1: Understand the purpose of filter
The filter method tests each element with a function and keeps only those that return true.
Step 2: Compare options with filter behavior
Only Creates a new array with elements that pass a test describes creating a new array with elements that pass the test, which matches filter's behavior.
Final Answer:
Creates a new array with elements that pass a test -> Option B
Quick Check:
filter creates new filtered array = C [OK]
Hint: Filter keeps items passing test, original array stays same [OK]
Common Mistakes:
Thinking filter changes original array
Confusing filter with sort or reduce
Believing filter combines elements
2. Which of the following is the correct syntax to filter an array arr for values greater than 10?
easy
A. arr.filter(x => {x > 10});
B. arr.filter(x > 10);
C. arr.filter(x => x > 10);
D. arr.filter(x => x > 10)();
Solution
Step 1: Check arrow function syntax inside filter
The filter method requires a function that returns true or false. The arrow function must return the condition directly or with a return statement.
Step 2: Validate each option's syntax
arr.filter(x => x > 10); uses correct arrow function syntax returning x > 10. arr.filter(x > 10); misses the function. arr.filter(x => {x > 10}); uses braces but no return, so returns undefined. arr.filter(x => x > 10)(); adds extra parentheses causing error.
Final Answer:
arr.filter(x => x > 10); -> Option C
Quick Check:
Correct arrow function syntax = A [OK]
Hint: Arrow function must return condition inside filter [OK]
Common Mistakes:
Omitting arrow function syntax
Using braces without return in arrow function
Adding extra parentheses after filter call
3. What is the output of the following code?
const nums = [5, 12, 8, 130, 44];
const result = nums.filter(num => num > 10);
console.log(result);
medium
A. [5, 8]
B. []
C. [5, 12, 8, 130, 44]
D. [12, 130, 44]
Solution
Step 1: Identify the filter condition
The filter keeps numbers greater than 10.
Step 2: Check each number in the array
5 (no), 12 (yes), 8 (no), 130 (yes), 44 (yes). So filtered array is [12, 130, 44].
Final Answer:
[12, 130, 44] -> Option D
Quick Check:
Filter > 10 = [12,130,44] [OK]
Hint: Filter keeps only items passing condition [OK]
Common Mistakes:
Including numbers not greater than 10
Returning original array instead of filtered
Confusing filter with map or reduce output
4. Find the error in this code that tries to filter even numbers: