Skip to content
Open
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
80 changes: 28 additions & 52 deletions store.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,10 +12,6 @@ interface Store {
products: Product[];
}

// ------------------------------------
// Store data (5 products)
// ------------------------------------

const store: Store = {
storeName: "My Online Store",
products: [
Expand Down Expand Up @@ -68,54 +53,45 @@ 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((p: Product) => 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(
(product) => product.price <= maxPrice && product.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(
(product)=> product.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(
(product)=> product.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(
(product) => cart.indexOf(product.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((product) => product.inStock)
.reduce(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work using the reduce

(sum: number, p: Product): number => sum + p.price,
0
);
}