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
72 changes: 19 additions & 53 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,35 @@ 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);
}

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[]{
return store.products.filter((prod)=>prod.tags.includes(tag));
}

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);
}

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;
}