From 3a35d53de75e53389c64e5995661220d0423ccea Mon Sep 17 00:00:00 2001 From: Elad Abutbul Date: Sat, 6 Dec 2025 16:31:05 +0200 Subject: [PATCH 1/2] elad-abutbul --- store.js | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ store.ts | 57 +++++++++++++++++++++-------------------- 2 files changed, 105 insertions(+), 29 deletions(-) create mode 100644 store.js diff --git a/store.js b/store.js new file mode 100644 index 0000000..ec75fab --- /dev/null +++ b/store.js @@ -0,0 +1,77 @@ +// 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. +// ------------------------------------ +// Store data (5 products) +// ------------------------------------ +var store = { + storeName: "My Online Store", + products: [ + { + id: 1, + name: "Wireless Mouse", + price: 80, + inStock: true, + tags: ["electronics", "mouse", "wireless"], + }, + { + id: 2, + name: "Mechanical Keyboard", + price: 250, + inStock: false, + tags: ["electronics", "keyboard", "gaming"], + }, + { + id: 3, + name: "USB-C Cable", + price: 40, + inStock: true, + tags: ["electronics", "cable", "accessories"], + }, + { + id: 4, + name: "Notebook", + price: 20, + inStock: true, + tags: ["stationery", "paper", "office"], + }, + { + id: 5, + name: "Gaming Headset", + price: 300, + inStock: true, + tags: ["electronics", "gaming", "audio"], + }, + ], +}; +// ------------------------------------ +// Shopping cart: array of product IDs +// ------------------------------------ +var cart = [1, 3, 5]; +// ------------------------------------ +// Functions to implement +// ------------------------------------ +function getAvailableProducts(store) { + return store.products.filter(function (p) { return p.inStock; }); +} +function getProductsInPriceRange(store, minPrice, maxPrice) { + return store.products.filter(function (p) { return p.price <= maxPrice && p.price >= minPrice; }); +} +function getProductsByTag(store, tag) { + return store.products.filter(function (p) { return p.tags.indexOf(tag) !== -1; }); +} +function getAvailableProductsByTag(store, tag) { + return getProductsByTag(store, tag).filter(function (p) { return p.inStock; }); +} +function getCartProducts(store, cart) { + return store.products.filter(function (p) { return cart.indexOf(p.id) !== -1; }); +} +function getCartTotalInStock(store, cart) { + return getCartProducts(store, cart) + .filter(function (p) { return p.inStock; }) + .reduce(function (sum, p) { return sum + p.price; }, 0); +} diff --git a/store.ts b/store.ts index 1745d61..f874ec9 100644 --- a/store.ts +++ b/store.ts @@ -78,44 +78,43 @@ 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((p: Product): boolean => p.inStock); } function getProductsInPriceRange( - store /* : add type here */, - minPrice /* : add type here */, - maxPrice /* : add type here */ -) /* : add return types */ { - return []; + store: Store, + minPrice: number, + maxPrice: number +): Product[] { + return store.products.filter( + (p: Product): boolean => p.price <= maxPrice && p.price >= minPrice + ); } -function getProductsByTag( - store /* : add type here */, - tag /* : add type here */ -) /* : add return types */ { - return []; +function getProductsByTag(store: Store, tag: string): Product[] { + return store.products.filter( + (p: Product): boolean => p.tags.indexOf(tag) !== -1 + ); } -function getAvailableProductsByTag( - store /* : add type here */, - tag /* : add type here */ -) /* : add return types */ { - return []; +function getAvailableProductsByTag(store: Store, tag: string): Product[] { + return getProductsByTag(store, tag).filter( + (p: Product): boolean => p.inStock + ); } -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( + (p: Product): boolean => cart.indexOf(p.id) !== -1 + ); } -function getCartTotalInStock( - store /* : add type here */, - cart /* : add type here */ -) /* : add return types */ { - return 0; +function getCartTotalInStock(store: Store, cart: number[]): number { + return getCartProducts(store, cart) + .filter((p: Product): boolean => p.inStock) + .reduce( + (sum: number, p: Product): number => sum + p.price, + 0 + ); } From 82f196d5293d32806eed90eba4b3239ebd31073c Mon Sep 17 00:00:00 2001 From: Elad Abutbul Date: Thu, 11 Dec 2025 20:26:50 +0200 Subject: [PATCH 2/2] fixed pl --- store.js | 77 -------------------------------------------------------- store.ts | 35 +++++--------------------- 2 files changed, 6 insertions(+), 106 deletions(-) delete mode 100644 store.js diff --git a/store.js b/store.js deleted file mode 100644 index ec75fab..0000000 --- a/store.js +++ /dev/null @@ -1,77 +0,0 @@ -// 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. -// ------------------------------------ -// Store data (5 products) -// ------------------------------------ -var store = { - storeName: "My Online Store", - products: [ - { - id: 1, - name: "Wireless Mouse", - price: 80, - inStock: true, - tags: ["electronics", "mouse", "wireless"], - }, - { - id: 2, - name: "Mechanical Keyboard", - price: 250, - inStock: false, - tags: ["electronics", "keyboard", "gaming"], - }, - { - id: 3, - name: "USB-C Cable", - price: 40, - inStock: true, - tags: ["electronics", "cable", "accessories"], - }, - { - id: 4, - name: "Notebook", - price: 20, - inStock: true, - tags: ["stationery", "paper", "office"], - }, - { - id: 5, - name: "Gaming Headset", - price: 300, - inStock: true, - tags: ["electronics", "gaming", "audio"], - }, - ], -}; -// ------------------------------------ -// Shopping cart: array of product IDs -// ------------------------------------ -var cart = [1, 3, 5]; -// ------------------------------------ -// Functions to implement -// ------------------------------------ -function getAvailableProducts(store) { - return store.products.filter(function (p) { return p.inStock; }); -} -function getProductsInPriceRange(store, minPrice, maxPrice) { - return store.products.filter(function (p) { return p.price <= maxPrice && p.price >= minPrice; }); -} -function getProductsByTag(store, tag) { - return store.products.filter(function (p) { return p.tags.indexOf(tag) !== -1; }); -} -function getAvailableProductsByTag(store, tag) { - return getProductsByTag(store, tag).filter(function (p) { return p.inStock; }); -} -function getCartProducts(store, cart) { - return store.products.filter(function (p) { return cart.indexOf(p.id) !== -1; }); -} -function getCartTotalInStock(store, cart) { - return getCartProducts(store, cart) - .filter(function (p) { return p.inStock; }) - .reduce(function (sum, p) { return sum + p.price; }, 0); -} diff --git a/store.ts b/store.ts index f874ec9..1d5745d 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,18 +53,10 @@ const store: Store = { ], }; -// ------------------------------------ -// Shopping cart: array of product IDs -// ------------------------------------ - const cart = [1, 3, 5]; -// ------------------------------------ -// Functions to implement -// ------------------------------------ - function getAvailableProducts(store: Store): Product[] { - return store.products.filter((p: Product): boolean => p.inStock); + return store.products.filter((p: Product) => p.inStock); } function getProductsInPriceRange( @@ -88,31 +65,31 @@ function getProductsInPriceRange( maxPrice: number ): Product[] { return store.products.filter( - (p: Product): boolean => p.price <= maxPrice && p.price >= minPrice + (product) => product.price <= maxPrice && product.price >= minPrice ); } function getProductsByTag(store: Store, tag: string): Product[] { return store.products.filter( - (p: Product): boolean => p.tags.indexOf(tag) !== -1 + (product)=> product.tags.indexOf(tag) !== -1 ); } function getAvailableProductsByTag(store: Store, tag: string): Product[] { return getProductsByTag(store, tag).filter( - (p: Product): boolean => p.inStock + (product)=> product.inStock ); } function getCartProducts(store: Store, cart: number[]): Product[] { return store.products.filter( - (p: Product): boolean => cart.indexOf(p.id) !== -1 + (product) => cart.indexOf(product.id) !== -1 ); } function getCartTotalInStock(store: Store, cart: number[]): number { return getCartProducts(store, cart) - .filter((p: Product): boolean => p.inStock) + .filter((product) => product.inStock) .reduce( (sum: number, p: Product): number => sum + p.price, 0