Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
package-lock.json
15 changes: 15 additions & 0 deletions product-inventory/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "product-inventory",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"devDependencies": {
"jest": "^29.7.0"
}
}
28 changes: 28 additions & 0 deletions product-inventory/products.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function calculateDiscount(price, discountRate) {
if (typeof price !== 'number' || typeof discountRate !== 'number') return null;
if (discountRate < 0 || discountRate > 1) return null;
// TODO: Implement logic
const discount = price *(1-discountRate);
return discount;
}

function filterProducts(products, callback) {
if (!Array.isArray(products) || typeof callback !== 'function') return [];
// TODO: Implement filtering logic

return products.filter(callback);
}

function sortInventory(inventory, key) {
if (!Array.isArray(inventory) || typeof key !== 'string') return [];
// TODO: Implement sorting logic
const sortedArray = [...inventory].sort((a,b) =>a[key] - b[key]);
return sortedArray;
}


module.exports = {
calculateDiscount : calculateDiscount,
filterProducts : filterProducts,
sortInventory : sortInventory
}
75 changes: 75 additions & 0 deletions product-inventory/tests/products.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const products = require('../products.js');

let inventory = [
{item : "peach", price : 10},
{item : "apple", price : 2},
{item : "orange", price : 3},
{item : "banana" , price : 4}
];



describe("calculate Discount", ()=>{
test("applies a valid discount rate", () => {
expect(products.calculateDiscount(100, 0.1)).toBe(90);
});

test("handles an invalid discount rate gracefully", () => {
expect(products.calculateDiscount(100, -0.1)).toBe(null);
});

test("handles edge case with price of 0", () => {
expect(products.calculateDiscount(0, 0.2)).toBe(0);
});
});

describe("filter products", ()=>{
test("filter products based on callback", ()=>{
const filtered = products.filterProducts(inventory, product => product.price >1 )
expect(filtered).toEqual([
{item : "peach", price : 10},
{item : "apple", price : 2},
{item : "orange", price : 3},
{item : "banana" , price : 4},

])
});

test("returns empty array if products is not an array ", ()=>{
expect(products.filterProducts("invalid",product => product.price >1)).toEqual([]);
});

test("return empty array if no products match",()=>{
expect(products.filterProducts(inventory, product => product.price ==1)).toEqual([]);
});

test("returns empty array if callback is not a function", ()=>{
expect(products.filterProducts(inventory,"not a function")).toEqual([]);
});


});

describe("sortInventory", ()=>{

test("sorts the inventory by price in ascending order", ()=>{

expect(products.sortInventory(inventory,"price")).toEqual([
{item : "apple", price : 2},
{item : "orange", price : 3},
{item : "banana", price : 4},
{item : "peach", price : 10}
]);

});

test("returns an empty array for invalid inventory",()=>{
expect(products.sortInventory("nothing","price")).toEqual([])
});

test("returns an empty array for invalid key",()=>{

expect(products.sortInventory(inventory,123)).toEqual([])
});

});