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
121 changes: 95 additions & 26 deletions practice1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,26 @@ Your goal is to write functions that interact with this data.
* 2. name of type string
* 3. category of type string
* 4. price of type number
*
*
* *************/

interface Product {
id: number;
name: string;
category: string;
price: number;
}
// ✅ Sample data to test your functions
const products: Product[] = [
{ id: 201, name: "Laptop", category: "Electronics", price: 999.99 },
{ id: 202, name: "T-shirt", category: "Clothing", price: 19.99 },
{ id: 203, name: "Coffee Maker", category: "Kitchen Appliances", price: 79.99 },
{ id: 204, name: "Running Shoes", category: "Footwear", price: 59.99 },
{ id: 205, name: "Bookshelf", category: "Furniture", price: 149.99 },
{ id: 201, name: "Laptop", category: "Electronics", price: 999.99 },
{ id: 202, name: "T-shirt", category: "Clothing", price: 19.99 },
{
id: 203,
name: "Coffee Maker",
category: "Kitchen Appliances",
price: 79.99,
},
{ id: 204, name: "Running Shoes", category: "Footwear", price: 59.99 },
{ id: 205, name: "Bookshelf", category: "Furniture", price: 149.99 },
];

/**************************************************************
Expand All @@ -34,54 +44,102 @@ Create a function `getProductName` that:
- Accepts a `product` of type `Product`
- Returns the name of the product
**************************************************************/

// console.log(getProductName(products[0])); // "Laptop"
function getProductName(product: Product): boolean {
if (product.name === "Laptop") {
return true;
} else {
return false;
}
}
const newProducts = products.filter(getProductName);
const productName = newProducts.map((item) => item.name);
console.log(productName); // "Laptop"

/**************************************************************
✅ Question 2:
Create a function `isProductCategoryMatching` that:
- Accepts a `product` of type `Product` and a `category` (string)
- Returns `true` if the product’s category matches the given category
**************************************************************/

// console.log(isProductCategoryMatching(products[1], "Clothing")); // true
function isProductCategoryMatching(
product: Product,
category: string
): boolean {
if (product.category === "Clothing") {
return true;
} else {
return false;
}
}
console.log(isProductCategoryMatching(products[1], "Clothing")); // true

/**************************************************************
✅ Question 3:
Create a function `addProduct` that:
- Accepts an array of `Product` and a new `Product`
- Adds the product to the array and returns the updated array
**************************************************************/

// const newProduct: Product = { id: 206, name: "Headphones", category: "Electronics", price: 149.99 };
// console.log(addProduct(products, newProduct));
// - Adds the product to the array and returns the updated array
// **************************************************************/
function addProduct(products: Product[], newProduct: Product): Product[] {
return [...products, newProduct];
}
const newProduct: Product = {
id: 206,
name: "Headphones",
category: "Electronics",
price: 149.99,
};
const updatedProducts = addProduct(products, newProduct);
console.log(updatedProducts);

/**************************************************************
✅ Question 4:
Create a function `countElectronicsProducts` that:
- Accepts an array of products
- Returns the number of products in the "Electronics" category
**************************************************************/

// console.log(countElectronicsProducts(products));
function countElectronicsProducts(prodects1: Product): boolean {
if (prodects1.category === "Electronics") {
return true;
} else {
return false;
}
}
const prodectsrFilter = products.filter(countElectronicsProducts);
prodectsrFilter.length;
console.log(prodectsrFilter.length);

/**************************************************************
✅ Question 5: 🌶️
Create a function `listProductNamesByCategory` that:
- Accepts an array of products and a category
- Returns an array of product names that match the category
**************************************************************/

// console.log(listProductNamesByCategory(products, "Electronics"));
function listProductNamesByCategory(
products: Product[],
category: string
): string[] {
const filteredProducts = products.filter(
(product) => product.category === category
);
const newProducts = filteredProducts.map((item) => item.name);
return newProducts;
// products.filter((product) => product.category === category).map((item) => item.name)
}

console.log(listProductNamesByCategory(products, "Electronics"));

/**************************************************************
✅ Question 6: 🌶️🌶️
Create a function `getCheapestProduct` that:
- Accepts an array of products
- Returns the product object with the lowest price
**************************************************************/

// console.log(getCheapestProduct(products));
function getCheapestProduct(products: Product[]): string[] {
return products
.filter((item) => item.price === 19.99)
.map((item) => item.name);
}
console.log(getCheapestProduct(products));

/**************************************************************
✅ Question 7:
Expand All @@ -90,14 +148,25 @@ Create a function `removeProductById` that:
- Removes the product with that ID
- Returns the updated array
**************************************************************/

// console.log(removeProductById(products, 202));
function removeProductById(products: Product[], productID: number): Product[] {
return products.filter((item) => item.id !== productID);
}
console.log(removeProductById(products, 202));

/**************************************************************
✅ Question 8: 🌶️🌶️🌶️
Create a function `getProductsByPriceRange` that:
- Accepts an array of products, a min price, and a max price
- Returns an array of products within that price range (inclusive)
**************************************************************/

// console.log(getProductsByPriceRange(products, 19.99, 59.99));
function getProductsByPriceRange(
products: Product[],
minPrice: number,
maxPrice: number
): string[] {
const productsPrice = products
.filter((item) => item.price >= minPrice && item.price <= maxPrice)
.map((item) => item.name);
return productsPrice;
}
console.log(getProductsByPriceRange(products, 19.99, 59.99));
113 changes: 0 additions & 113 deletions tsconfig.json

This file was deleted.