From d904f7add3756611684642c780fa09cb61d60b40 Mon Sep 17 00:00:00 2001 From: sltan29 Date: Thu, 4 Dec 2025 02:16:00 +0200 Subject: [PATCH 1/2] wrote all methods, added type to every parameter and every return type. --- store.ts | 77 ++++++++++++++++++-------------------------------------- 1 file changed, 24 insertions(+), 53 deletions(-) diff --git a/store.ts b/store.ts index 1745d61..18d91d9 100644 --- a/store.ts +++ b/store.ts @@ -1,14 +1,3 @@ -// store.ts -// Homework - Online Store System -// ------------------------------------ -// Instructions: -// - Add a type for every function parameter. -// - Add an explicit return type for every function (even if it is void). -// - Make sure the code compiles without TypeScript errors. - -// ------------------------------------ -// Interfaces -// ------------------------------------ interface Product { id: number; @@ -23,10 +12,6 @@ interface Store { products: Product[]; } -// ------------------------------------ -// Store data (5 products) -// ------------------------------------ - const store: Store = { storeName: "My Online Store", products: [ @@ -68,54 +53,40 @@ const store: Store = { ], }; -// ------------------------------------ -// Shopping cart: array of product IDs -// ------------------------------------ - const cart = [1, 3, 5]; -// ------------------------------------ -// Functions to implement -// ------------------------------------ - -function getAvailableProducts( - store /* : add type here */ -) /* : add return types */ { - return []; +function getAvailableProducts(store : Store) : Product[] { + return store.products.filter((product)=>product.inStock===true); } -function getProductsInPriceRange( - store /* : add type here */, - minPrice /* : add type here */, - maxPrice /* : add type here */ -) /* : add return types */ { - return []; +function getProductsInPriceRange(store : Store, minPrice : number ,maxPrice : number ) : Product[] { + return store.products.filter((product)=> product.price>=minPrice && product.price<=maxPrice); } -function getProductsByTag( - store /* : add type here */, - tag /* : add type here */ -) /* : add return types */ { - return []; +function getProductsByTag(store : Store ,tag : string) : Product[]{ + const prodWithTag : Product[] = []; + store.products.forEach((product : Product) =>{ + if (product.tags.includes(tag)) + prodWithTag.push(product); + }); + return prodWithTag; } -function getAvailableProductsByTag( - store /* : add type here */, - tag /* : add type here */ -) /* : add return types */ { - return []; +function getAvailableProductsByTag(store : Store, tag : string) : Product[] { + const prodByTag : Product[] = getProductsByTag(store, tag); + return prodByTag.filter((product) => product.inStock===true); } -function getCartProducts( - store /* : add type here */, - cart /* : add type here */ -) /* : add return types */ { - return []; +function getCartProducts(store : Store , cart : number[] ) : Product[] { + return store.products.filter((product) => cart.includes(product.id)); } -function getCartTotalInStock( - store /* : add type here */, - cart /* : add type here */ -) /* : add return types */ { - return 0; +function getCartTotalInStock(store : Store ,cart : number[]) : number{ + const productsToSum : Product[] = store.products.filter((product) => cart.includes(product.id) && product.inStock === true); + let sum = 0; + productsToSum.forEach((product) => { + sum+=product.price; + }) + return sum; } + From e7012479c303d4a4bad5158641a06239812f6e99 Mon Sep 17 00:00:00 2001 From: sltan29 Date: Sat, 13 Dec 2025 00:14:39 +0200 Subject: [PATCH 2/2] removed unnecessary ===true and made getProductsByTag more readable --- store.ts | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/store.ts b/store.ts index 18d91d9..8bec304 100644 --- a/store.ts +++ b/store.ts @@ -56,7 +56,7 @@ const store: Store = { const cart = [1, 3, 5]; function getAvailableProducts(store : Store) : Product[] { - return store.products.filter((product)=>product.inStock===true); + return store.products.filter((product)=>product.inStock); } function getProductsInPriceRange(store : Store, minPrice : number ,maxPrice : number ) : Product[] { @@ -64,17 +64,12 @@ function getProductsInPriceRange(store : Store, minPrice : number ,maxPrice : nu } function getProductsByTag(store : Store ,tag : string) : Product[]{ - const prodWithTag : Product[] = []; - store.products.forEach((product : Product) =>{ - if (product.tags.includes(tag)) - prodWithTag.push(product); - }); - return prodWithTag; + return store.products.filter((prod)=>prod.tags.includes(tag)); } function getAvailableProductsByTag(store : Store, tag : string) : Product[] { const prodByTag : Product[] = getProductsByTag(store, tag); - return prodByTag.filter((product) => product.inStock===true); + return prodByTag.filter((product) => product.inStock); } function getCartProducts(store : Store , cart : number[] ) : Product[] {