Issue #2 - Handling Ecommerce Data with Arrays & Objects
JS Bytes Issue #2 - Become a better JS engineer
Welcome to the second issue of JS Bytes! You can read the first issue here.
Today, we’re focusing on a key skill for every JavaScript developer: working with data.
Arrays and objects are the foundation of data in JavaScript. Mastering them is crucial to handling information in your code.
Whether you're just starting or sharpening your skills, this issue will help you confidently understand and use these essential concepts.
JavaScript Problem Solving Challenge
You are given an array of objects (products) representing items from a dummy e-commerce store.
Each product object contains the following fields:
id
name
category
price
stock
sales
Your task is to solve the following 3 problems using this product database.
Ecommerce Products Database
const products = [
{
id: 1,
name: 'Wireless Mouse',
category: 'Electronics',
price: 28.95,
stock: 34,
sales: 150,
},
{
id: 2,
name: 'Bluetooth Headphones',
category: 'Electronics',
price: 95.5,
stock: 12,
sales: 200,
},
{
id: 3,
name: 'Laptop',
category: 'Electronics',
price: 910.75,
stock: 7,
sales: 50,
},
{
id: 4,
name: 'Coffee Maker',
category: 'Home Appliances',
price: 45.3,
stock: 0,
sales: 85,
},
{
id: 5,
name: 'Smartphone',
category: 'Electronics',
price: 680.4,
stock: 25,
sales: 300,
},
{
id: 6,
name: 'Blender',
category: 'Home Appliances',
price: 37.6,
stock: 10,
sales: 45,
},
{
id: 7,
name: 'Desk Lamp',
category: 'Home Decor',
price: 18.8,
stock: 50,
sales: 70,
},
{
id: 8,
name: 'Office Chair',
category: 'Furniture',
price: 85.2,
stock: 5,
sales: 60,
},
{
id: 9,
name: 'Electric Kettle',
category: 'Home Appliances',
price: 23.7,
stock: 0,
sales: 90,
},
{
id: 10,
name: 'Running Shoes',
category: 'Footwear',
price: 57.45,
stock: 20,
sales: 120,
},
{
id: 11,
name: 'Air Conditioner',
category: 'Home Appliances',
price: 389.95,
stock: 8,
sales: 30,
},
{
id: 12,
name: 'Bookshelf',
category: 'Furniture',
price: 142.3,
stock: 15,
sales: 40,
},
{
id: 13,
name: 'Smartwatch',
category: 'Electronics',
price: 192.99,
stock: 18,
sales: 75,
},
{
id: 14,
name: 'Electric Toothbrush',
category: 'Personal Care',
price: 27.55,
stock: 22,
sales: 55,
},
{
id: 15,
name: 'Yoga Mat',
category: 'Sports',
price: 17.2,
stock: 30,
sales: 100,
},
{
id: 16,
name: 'Gaming Console',
category: 'Electronics',
price: 489.0,
stock: 10,
sales: 95,
},
{
id: 17,
name: 'Water Bottle',
category: 'Sports',
price: 9.5,
stock: 40,
sales: 85,
},
{
id: 18,
name: 'Microwave Oven',
category: 'Home Appliances',
price: 94.75,
stock: 8,
sales: 60,
},
{
id: 19,
name: 'T-Shirt',
category: 'Apparel',
price: 13.99,
stock: 60,
sales: 110,
},
{
id: 20,
name: 'Jeans',
category: 'Apparel',
price: 47.3,
stock: 20,
sales: 75,
},
]
1. Group Products by Category
Problem:
Write a function called groupProductsByCategory
that takes the products
array as a parameter and returns an object where the keys are category names, and the values are arrays containing the products that belong to that category.
Clarification:
Each product should be grouped under its respective category.
Return an object where each category name is a key, and the value is an array of product objects.
function groupProductsByCategory(products) {
// Your code here
}
Sample Output:
{
"Electronics": [
{ id: 1, name: "Wireless Mouse", ... },
{ id: 2, name: "Bluetooth Headphones", ... },
...
],
"Home Appliances": [
{ id: 4, name: "Coffee Maker", ... },
...
],
"Furniture": [
{ id: 8, name: "Office Chair", ... },
...
]
}
2. Get Top N Selling Products
Problem:
Write a function called topSellingProducts
that accepts two parameters: the products
array and a number n
representing the number of top-selling products you want to return. The function should return an array of the top N products sorted by their sales in descending order.
Clarification:
Sort the products by their
sales
property.Return the top
n
products with the highest sales.
function topSellingProducts(products, n) {
// Your code here
}
Sample Output (for n = 3):
[
{ id: 5, name: "Smartphone", sales: 300, ... },
{ id: 2, name: "Bluetooth Headphones", sales: 200, ... },
{ id: 1, name: "Wireless Mouse", sales: 150, ... }
]
3. Get Products with High Sales but Low Stock
Problem:
Write a function called getHighSalesLowStockProducts
that takes three parameters: products
, salesThreshold
, and stockThreshold
. The function should return an array of products that have sales greater than or equal to salesThreshold
and stock less than or equal to stockThreshold
.
Clarification:
Only include products where the sales exceed or meet the
salesThreshold
and the stock is below or equal to thestockThreshold
.Return the filtered array of products.
function getHighSalesLowStockProducts(
products,
salesThreshold,
stockThreshold,
) {
// Your code here
}
Sample Output (for salesThreshold = 100, stockThreshold = 10):
[
{ id: 16, name: "Gaming Console", sales: 95, stock: 10, ... },
{ id: 9, name: "Electric Kettle", sales: 90, stock: 0, ... }
]
3 Conceptual MCQs
1. What will be the output of the following code snippet?
const welcome = {
name: 'Ali',
greet: function () {
const that = this
return () => {
console.log(`Hello, ${that.name}`)
}
},
}
const greetFunction = welcome.greet()
greetFunction()
A) Hello, undefined
B) Hello, Ali
C) TypeError: greetFunction is not a function
D) ReferenceError: that is not defined
2. What will be the output of the following code?
const arr = [1, , 3, , 5]
arr[10] = 10
console.log(arr.length)
A) 4
B) 6
C) 10
D) 11
3. What's the output?
let num = 0
for (let i = 1; i <= 5; i++) {
if (i % 2 === 0) continue
num += i
}
console.log(num)
A) 6
B) 9
C) 12
D) 15
If you enjoyed this post, I’d love it if you:
Subscribe if you haven’t
Share this with fellow developers
Here you can read the first issue. Feel free to connect with me on LinkedIn and share your thoughts.