LearnFrenzy
Sign in Take a free test

Arrow Functions · Arrow Functions Coding Problems · Question 5

Filter and sort products with arrow functions in JavaScript Solution, output & explanation

  • Program
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 5 Back to Exercise

From a product list, show in-stock products under ₹2,000, cheapest first, as "name – ₹price" strings, using only arrow callbacks.

Solution
main.js
const products = [
  { name: "Earbuds", price: 1499, stock: 12 },
  { name: "Speaker", price: 2999, stock: 4 },
  { name: "Cable", price: 299, stock: 0 },
  { name: "Charger", price: 899, stock: 30 },
  { name: "Stand", price: 1199, stock: 7 },
];

const affordable = products
  .filter(({ price, stock }) => stock > 0 && price < 2000)
  .toSorted((a, b) => a.price - b.price)
  .map(({ name, price }) => `${name} – ₹${price}`);

console.log(affordable.join("\n"));

Output Verified by running it on Node.js

Charger – ₹899
Stand – ₹1199
Earbuds – ₹1499

Arrow callbacks with parameter destructuring keep each step to one readable line; toSorted keeps the original list unchanged.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links