From 5d313f4f43265024fb578c2f2890fea368a67136 Mon Sep 17 00:00:00 2001 From: Saad Abdul Hamid Parkar Date: Fri, 2 May 2025 06:37:26 +0300 Subject: [PATCH] solved --- practice1.ts | 192 +++++++++++++++++++++++++++++++++------------------ practice2.ts | 177 ++++++++++++++++++++++++++++------------------- practice3.ts | 147 ++++++++++++++++++++++++++------------- 3 files changed, 327 insertions(+), 189 deletions(-) diff --git a/practice1.ts b/practice1.ts index 85eaf5e..2617ece 100644 --- a/practice1.ts +++ b/practice1.ts @@ -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)); \ No newline at end of file + ✅ 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)); diff --git a/practice2.ts b/practice2.ts index d854cf0..b8e6a7c 100644 --- a/practice2.ts +++ b/practice2.ts @@ -10,69 +10,81 @@ * 3. category of type string * 4. price of type number * 5. ingridents of type array of strings - * + * * *************/ +interface MenuItem { + id: number; + name: string; + category: string; + price: number; + ingredients: string[]; +} // ✅ Step 2: Use this array to test your answers const menu: MenuItem[] = [ - { - id: 301, - name: "Pasta", - category: "Main Course", - price: 12.99, - ingredients: ["Penne", "Tomato Sauce", "Parmesan", "Basil"], - }, - { - id: 302, - name: "Caesar Salad", - category: "Salad", - price: 8.99, - ingredients: ["Lettuce", "Croutons", "Caesar Dressing", "Parmesan"], - }, - { - id: 303, - name: "Burger", - category: "Main Course", - price: 10.99, - ingredients: ["Beef Patty", "Lettuce", "Tomato", "Cheese", "Bun"], - }, - { - id: 304, - name: "French Fries", - category: "Side Dish", - price: 4.99, - ingredients: ["Potatoes", "Salt", "Oil"], - }, - { - id: 305, - name: "Cheesecake", - category: "Dessert", - price: 6.99, - ingredients: ["Cream Cheese", "Graham Cracker Crust", "Strawberries"], - }, + { + id: 301, + name: "Pasta", + category: "Main Course", + price: 12.99, + ingredients: ["Penne", "Tomato Sauce", "Parmesan", "Basil"], + }, + { + id: 302, + name: "Caesar Salad", + category: "Salad", + price: 8.99, + ingredients: ["Lettuce", "Croutons", "Caesar Dressing", "Parmesan"], + }, + { + id: 303, + name: "Burger", + category: "Main Course", + price: 10.99, + ingredients: ["Beef Patty", "Lettuce", "Tomato", "Cheese", "Bun"], + }, + { + id: 304, + name: "French Fries", + category: "Side Dish", + price: 4.99, + ingredients: ["Potatoes", "Salt", "Oil"], + }, + { + id: 305, + name: "Cheesecake", + category: "Dessert", + price: 6.99, + ingredients: ["Cream Cheese", "Graham Cracker Crust", "Strawberries"], + }, ]; - +console.log("--------------------------------------Q1-----------------------------------------"); /************************************* ✅ Question 1: Create a function `getMenuItemName` that: - Accepts a `MenuItem` - Returns the name of the item -*************************************/ - - -// console.log(getMenuItemName(menu[0])); + *************************************/ +function getMenuItemName(item: MenuItem): string { + return item.name; +} +console.log(getMenuItemName(menu[0])); +console.log("--------------------------------------Q2-----------------------------------------"); /************************************* ✅ Question 2: Create a function `isMenuItemInCategory` that: - Accepts a `MenuItem` and a `category` string - Returns true if the item belongs to that category -*************************************/ - -// console.log(isMenuItemInCategory(menu[1], "Salad")); + *************************************/ +function isMenuItemInCategory(item: MenuItem, category: string): boolean { + return item.category === category; +} +console.log(isMenuItemInCategory(menu[1], "Salad")); +console.log("--------------------------------------Q3-----------------------------------------"); /************************************* ✅ Question 3: @@ -80,55 +92,77 @@ const menu: MenuItem[] = [ - Accepts a `MenuItem[]` array and a new `MenuItem` - Adds the new item to the array - Returns the updated array -*************************************/ - - -// const newMenuItem: MenuItem = { -// id: 306, -// name: "Pizza", -// category: "Main Course", -// price: 14.99, -// ingredients: ["Dough", "Tomato Sauce", "Mozzarella", "Pepperoni"], -// }; -// console.log(addMenuItem(menu, newMenuItem)); - + *************************************/ +function addMenuItem(menu: MenuItem[], newItem: MenuItem): MenuItem[] { + menu.push(newItem); + return menu; +} + +const newMenuItem: MenuItem = { + id: 306, + name: "Pizza", + category: "Main Course", + price: 14.99, + ingredients: ["Dough", "Tomato Sauce", "Mozzarella", "Pepperoni"], +}; +console.log(addMenuItem(menu, newMenuItem)); +console.log("--------------------------------------Q4-----------------------------------------"); /************************************* ✅ Question 4: Create a function `countMainCourseItems` that: - Accepts a `MenuItem[]` array - Returns how many items are in the "Main Course" category -*************************************/ + *************************************/ +function countMainCourseItems(menu: MenuItem[]): number { + let courseMenu = menu.filter((item) => item.category === "Main Course").length; + return courseMenu; +} -// console.log(countMainCourseItems(menu)); +console.log(countMainCourseItems(menu)); +console.log("--------------------------------------Q5-----------------------------------------"); /************************************* ✅ Question 5: Create a function `removeMenuItemById` that: - Accepts a `MenuItem[]` and a `menuItemId` number - Returns a new array without the item that matches the ID -*************************************/ + *************************************/ +function removeMenuItemById(menu: MenuItem[], menuItemId: number): MenuItem[] { + let removeByID = menu.filter((item) => item.id !== menuItemId); + return removeByID; +} +console.log(removeMenuItemById(menu, 302)); +console.log("--------------------------------------Q6-----------------------------------------"); /************************************* ✅ Question 6: 🌶️ Create a function `listMenuItemNamesByCategory` that: - Accepts a `MenuItem[]` and a `category` string - Returns an array of item names that belong to that category -*************************************/ - -// console.log(listMenuItemNamesByCategory(menu, "Main Course")); - - + *************************************/ +function listMenuItemNamesByCategory(menu: MenuItem[], category: string): string[] { + let listMenuItem = menu.filter((item) => item.category === category).map((item) => item.name); + return listMenuItem; +} + +console.log(listMenuItemNamesByCategory(menu, "Main Course")); +console.log("--------------------------------------Q7-----------------------------------------"); /************************************* ✅ Question 7: 🌶️🌶️ Create a function `getCheapestMenuItem` that: - Accepts a `MenuItem[]` array - Returns the menu item with the lowest price -*************************************/ + *************************************/ +function getCheapestMenuItem(menu: MenuItem[]): MenuItem { + let chaepestItem = menu.reduce((cheapest, current) => (current.price < cheapest.price ? current : cheapest)); -// console.log(getCheapestMenuItem(menu)); + return chaepestItem; +} +console.log(getCheapestMenuItem(menu)); +console.log("--------------------------------------Q8-----------------------------------------"); /************************************* ✅ Question 8: 🌶️🌶️🌶️ @@ -136,10 +170,9 @@ const menu: MenuItem[] = [ - Accepts a `MenuItem[]` and an `ingredient` string - Returns items that include that ingredient *************************************/ +function getMenuItemsByIngredient(menu: MenuItem[], ingredient: string): MenuItem[] { + let getItemByIngredient = menu.filter((item) => item.ingredients.includes(ingredient)); + return getItemByIngredient; +} -// console.log(getMenuItemsByIngredient(menu, "Parmesan")); - - - - -// console.log(removeMenuItemById(menu, 302)); \ No newline at end of file +console.log(getMenuItemsByIngredient(menu, "Parmesan")); diff --git a/practice3.ts b/practice3.ts index 17e67de..4166956 100644 --- a/practice3.ts +++ b/practice3.ts @@ -14,77 +14,128 @@ Your goal is to write functions that analyze and transform this data. // ✅ Do not change this interface interface Employee { - id: number; - name: string; - department: string; - salary: number; + id: number; + name: string; + department: string; + salary: number; } // ✅ Sample data const employees: Employee[] = [ - { id: 101, name: "Alice", department: "Engineering", salary: 7000 }, - { id: 102, name: "Bob", department: "Marketing", salary: 5000 }, - { id: 103, name: "Charlie", department: "Engineering", salary: 7200 }, - { id: 104, name: "Diana", department: "HR", salary: 4800 }, - { id: 105, name: "Ethan", department: "Marketing", salary: 5300 }, - { id: 106, name: "Fay", department: "Engineering", salary: 6900 }, - { id: 107, name: "George", department: "HR", salary: 5100 }, + { id: 101, name: "Alice", department: "Engineering", salary: 7000 }, + { id: 102, name: "Bob", department: "Marketing", salary: 5000 }, + { id: 103, name: "Charlie", department: "Engineering", salary: 7200 }, + { id: 104, name: "Diana", department: "HR", salary: 4800 }, + { id: 105, name: "Ethan", department: "Marketing", salary: 5300 }, + { id: 106, name: "Fay", department: "Engineering", salary: 6900 }, + { id: 107, name: "George", department: "HR", salary: 5100 }, ]; +console.log("--------------------------------------Q1-----------------------------------------"); +/************************************************************** + ✅ Q1) getSortedEmployeesBySalary(department: string): + - Accepts a department name (string) + - Returns an array of employees from that department, + sorted by salary in descending order + **************************************************************/ +function getSortedEmployeesBySalary(department: string): Employee[] { + let fileredEmployees = employees.filter((emp) => emp.department === department).sort((a, b) => b.salary - a.salary); + return fileredEmployees; +} +console.log(getSortedEmployeesBySalary("Engineering")); -/************************************************************** -✅ Q1) getSortedEmployeesBySalary(department: string): -- Accepts a department name (string) -- Returns an array of employees from that department, - sorted by salary in descending order -**************************************************************/ +// Should show Charlie (7200), Alice (7000), Fay (6900) // console.log(getSortedEmployeesBySalary("Engineering")); - +console.log("--------------------------------------Q2-----------------------------------------"); /************************************************************** -✅ Q2) promoteEmployee(employeeId: number, amount: number): -- Accepts an employee ID and a raise amount -- Returns a new version of the employee with the updated salary -- If no employee is found, return undefined -**************************************************************/ + ✅ Q2) promoteEmployee(employeeId: number, amount: number): + - Accepts an employee ID and a raise amount + - Returns a new version of the employee with the updated salary + - If no employee is found, return undefined + **************************************************************/ +function promoteEmployee(employeeId: number, amount: number): Employee | undefined { + const emp = employees.find((emp) => emp.id === employeeId); + let promoteEmployee = emp ? { ...emp, salary: emp.salary + amount } : undefined; + return promoteEmployee; +} -// console.log(promoteEmployee(102, 500)); +console.log(promoteEmployee(102, 500)); + +// Should return Bob with salary 5500 +// console.log(promoteEmployee(102, 500)); +console.log("--------------------------------------Q3-----------------------------------------"); /************************************************************** -✅ Question 3: 🌶️ -Create a function `getHighestPaidEmployee` that: -- Accepts an array of employees -- Returns the employee with the highest salary -**************************************************************/ + ✅ Question 3: 🌶️ + Create a function `getHighestPaidEmployee` that: + - Accepts an array of employees + - Returns the employee with the highest salary + **************************************************************/ +function getHighestPaidEmployee(employees: Employee[]): Employee { + let getHighestPaidEmployee = employees.reduce((highest, current) => (current.salary > highest.salary ? current : highest)); + return getHighestPaidEmployee; +} -// console.log(getHighestPaidEmployee(employees)); // Charlie +console.log(getHighestPaidEmployee(employees)); +// Should return Charlie (salary 7200) +// console.log(getHighestPaidEmployee(employees)); // Charlie +console.log("--------------------------------------Q4-----------------------------------------"); /************************************************************** -✅ Q4) filterBySalaryRange(min: number, max: number): -- Returns employees whose salaries are within the given range (inclusive) -- Sorted in ascending order of salary -**************************************************************/ + ✅ Q4) filterBySalaryRange(min: number, max: number): + - Returns employees whose salaries are within the given range (inclusive) + - Sorted in ascending order of salary + **************************************************************/ +function filterBySalaryRange(min: number, max: number): Employee[] { + let filterBySalaryRange = employees.filter((emp) => emp.salary >= min && emp.salary <= max).sort((a, b) => a.salary - b.salary); + return filterBySalaryRange; +} -// console.log(filterBySalaryRange(5000, 7100)); +console.log(filterBySalaryRange(5000, 7100)); +// Should include Bob, Ethan, George, Alice +// console.log(filterBySalaryRange(5000, 7100)); + +console.log("--------------------------------------Q5-----------------------------------------"); /************************************************************** -✅ Question 5: 🌶️🌶️🌶️ -Create a function `groupEmployeesByDepartment` that: -- Accepts an array of employees -- Returns an object where each key is a department name, - and the value is an array of employee names in that department - -Example output: -{ - Engineering: ["Alice", "Charlie"], - Marketing: ["Bob", "Ethan"], - HR: ["Diana"] + ✅ Question 5: 🌶️🌶️🌶️ + Create a function `groupEmployeesByDepartment` that: + - Accepts an array of employees + - Returns an object where each key is a department name, + and the value is an array of employee names in that department + + Example output: + { + Engineering: ["Alice", "Charlie"], + Marketing: ["Bob", "Ethan"], + HR: ["Diana"] + } + **************************************************************/ +function groupEmployeesByDepartment(employees: Employee[]): Record { + let groupEmployeesByDepartment = employees.reduce((acc, emp) => { + (acc[emp.department] ||= []).push(emp.name); + return acc; + }, {} as Record); + return groupEmployeesByDepartment; } -**************************************************************/ -// console.log(groupEmployeesByDepartment(employees)); \ No newline at end of file +console.log(groupEmployeesByDepartment(employees)); + +// console.log(groupEmployeesByDepartment(employees)); + +/* + Expected Output: + { + Engineering: ["Alice", "Charlie", "Fay"], + Marketing: ["Bob", "Ethan"], + HR: ["Diana", "George"] + } + */ + +// console.log(groupEmployeesByDepartment(employees));