Product Search and Filter

Build search and filter helpers for a product list.

Start building, submit solution and get feedback from the community.
2Submit Solution
5 upvotes10 upvotes

Product Search and Filter

You are building search helpers for a small product list. Some helpers should return multiple products, and one helper should return a single product.

Write these functions:

  • filterByCategory(products, category) should return products in the matching category.

  • filterByMaxPrice(products, maxPrice) should return products at or below the max price.

  • getInStockProducts(products) should return products where inStock is true.

  • findProductById(products, productId) should return one matching product or undefined.

  • searchProducts(products, searchText) should return products whose name includes the search text, ignoring casing.

Sample checks:

js
const products = [  { id: 1, name: 'Notebook', category: 'stationery', price: 10, inStock: true },  { id: 2, name: 'Desk Lamp', category: 'home', price: 35, inStock: false },  { id: 3, name: 'Pen Set', category: 'stationery', price: 6, inStock: true },  {    id: 4,    name: 'Water Bottle',    category: 'fitness',    price: 18,    inStock: true,  },];console.log(filterByCategory(products, 'stationery').map((product) => product.name));console.log(filterByMaxPrice(products, 20).map((product) => product.name));console.log(findProductById(products, 3));console.log(searchProducts(products, 'pen').map((product) => product.name));console.log(getInStockProducts(products).map((product) => product.name));console.log(findProductById(products, 99));

Expected output:

txt
["Notebook", "Pen Set"]["Notebook", "Pen Set", "Water Bottle"]{ id: 3, name: "Pen Set", category: "stationery", price: 6, inStock: true }["Pen Set"]["Notebook", "Pen Set", "Water Bottle"]undefined

Search should be case-insensitive. The original products array should not be changed.

Join the Community

roadmap.sh is the most starred project on GitHub and is visited by hundreds of thousands of developers every month.

Rank  out of 28M!

361K

GitHub Stars

Star us on GitHub
Help us reach #1

+90k every month

+2.1M

Registered Users

Register yourself
Commit to your growth

+2k every month

49K

Discord Members

Join on Discord
Join the community

Roadmaps Best Practices Guides Videos FAQs YouTube

roadmap.sh by @kamrify

Community created roadmaps, best practices, projects, articles, resources and journeys to help you choose your path and grow in your career.

ThewNewStack

The top DevOps resource for Kubernetes, cloud-native computing, and large-scale development and deployment.