Skip to content

Instantly share code, notes, and snippets.

@massoudasadi
Created November 21, 2022 21:01
Show Gist options
  • Save massoudasadi/63c884a1859bb56d47c8fd9810306fb6 to your computer and use it in GitHub Desktop.
Save massoudasadi/63c884a1859bb56d47c8fd9810306fb6 to your computer and use it in GitHub Desktop.
HomeWork
// LTR
const pipe = (...fns: Function[]) => (x: any) => fns.reduce((res, fn) => fn(res), x);
// RTL
const compose = (...fns: Function[]) => (x: any) => fns.reduceRight((res, fn) => fn(res), x);
///////////////// Word Counter /////////////////////////////////////////////////////////
const text: string = " Hi, this is an Advanced !? 'text'\r\nAnother Line a \t b c a-a bc-d +.';";
// removes whitespace from both ends of a string
const trimText = (text: string) => text.trim();
// spaces delimit words
const splitText = (text: string) => text.split(/\s+/);
// count text
const countText = (textarray: string[]) => textarray.length;
// compose
const wordCount = compose(trimText, splitText, countText);
// print
console.log(wordCount(text));
///////////////// Basket /////////////////////////////////////////////////////////
interface Basket {
basketId: string,
userId: number,
discount: number,
productList: TempProduct[],
productListPrice? : number
}
interface TempProduct {
name: string,
price: number,
count: number
discount: number,
}
const basket: Basket = {
basketId: "",
userId: 3321,
discount: 500,
productList: [
{ name: "phone", price: 500, count: 2, discount: 20 },
{ name: "tablet", price: 600, count: 3, discount: 30 }
]
}
// calculate total price of products in basket
const basketProductListPrice = (basket: Basket) =>
{
basket.productListPrice! = basket.productList.reduce((previousProduct, currentProduct) =>
previousProduct + ((currentProduct.price * currentProduct.count) - currentProduct.discount), 0)
return basket
}
// calculate basket discount
const basketAfterDiscount = (basket: Basket) => basket.productListPrice! - basket.discount;
// compose
const basketPrice = compose(basketProductListPrice, basketAfterDiscount);
// print
console.log(basketPrice(basket));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment