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
192 changes: 123 additions & 69 deletions practice1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,88 +16,142 @@ 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 },
];

console.log("--------------------------------------Q1-----------------------------------------");
/**************************************************************
✅ Question 1:
Create a function `getProductName` that:
- Accepts a `product` of type `Product`
- Returns the name of the product
**************************************************************/

// console.log(getProductName(products[0])); // "Laptop"

✅ Question 1:
Create a function `getProductName` that:
- Accepts a `product` of type `Product`
- Returns the name of the product
**************************************************************/
function getProductName(product: Product): string {
return product.name;
}
console.log(getProductName(products[0])); // "Laptop"

console.log("--------------------------------------Q2-----------------------------------------");
/**************************************************************
✅ 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

✅ 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
**************************************************************/
function isProductCategoryMatching(product: Product, category: string): boolean {
if (product.category === category) {
return true;
}
return false;
}
console.log(isProductCategoryMatching(products[1], "Clothing")); // true

console.log("--------------------------------------Q3-----------------------------------------");
/**************************************************************
✅ 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));
✅ 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
**************************************************************/
function addProduct(products: Product[], newProduct: Product): Product[] {
products.push(newProduct);
return products;
}

/**************************************************************
✅ Question 4:
Create a function `countElectronicsProducts` that:
- Accepts an array of products
- Returns the number of products in the "Electronics" category
**************************************************************/
const newProduct: Product = { id: 206, name: "Headphones", category: "Electronics", price: 149.99 };

// console.log(countElectronicsProducts(products));
console.log(addProduct(products, newProduct));

console.log("--------------------------------------Q4-----------------------------------------");
/**************************************************************
✅ 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"));

✅ Question 4:
Create a function `countElectronicsProducts` that:
- Accepts an array of products
- Returns the number of products in the "Electronics" category
**************************************************************/
function countElectronicsProducts(product: Product[]): number {
let countElectronicsProduct = product.filter((product) => {
if (product.category === "Electronics") {
return true;
} else {
return false;
}
});
return countElectronicsProduct.length;
}
console.log(countElectronicsProducts(products));

console.log("--------------------------------------Q5-----------------------------------------");
/**************************************************************
✅ Question 6: 🌶️🌶️
Create a function `getCheapestProduct` that:
- Accepts an array of products
- Returns the product object with the lowest price
**************************************************************/

// console.log(getCheapestProduct(products));

✅ 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
**************************************************************/
function listProductNamesByCategory(products: Product[], category: string): string[] {
let namesByCategories = products.filter((product) => product.category === category).map((product) => product.name);
return namesByCategories;
}

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

console.log("--------------------------------------Q6-----------------------------------------");
/**************************************************************
✅ Question 7:
Create a function `removeProductById` that:
- Accepts an array of products and a product ID
- Removes the product with that ID
- Returns the updated array
**************************************************************/

// console.log(removeProductById(products, 202));

✅ Question 6: 🌶️🌶️
Create a function `getCheapestProduct` that:
- Accepts an array of products
- Returns the product object with the lowest price
**************************************************************/
function getCheapestProduct(products: Product[]): {} {
let sort = products.sort((a, b) => {
return a.price - b.price;
});
return sort[0];
}

console.log(getCheapestProduct(products));

console.log("--------------------------------------Q7-----------------------------------------");
/**************************************************************
✅ 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));
✅ Question 7:
Create a function `removeProductById` that:
- Accepts an array of products and a product ID
- Removes the product with that ID
- Returns the updated array
**************************************************************/
function removeProductById(products: Product[], id: number): Product[] {
let removeByID = products.filter((product) => product.id !== id);
return removeByID;
}

console.log(removeProductById(products, 202));

console.log("--------------------------------------Q8-----------------------------------------");
/**************************************************************
✅ 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)
**************************************************************/
function getProductsByPriceRange(products: Product[], min: number, max: number): Product[] {
let getByRange = products.filter((product) => product.price >= min && product.price <= max);
return getByRange;
}

console.log(getProductsByPriceRange(products, 19.99, 59.99));
Loading