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
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"));
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!