Skip to content

Instantly share code, notes, and snippets.

@gdyrrahitis

gdyrrahitis/order.js Secret

Created Jul 10, 2019
Embed
What would you like to do?
const keyboard = { name: 'Keyboard', inStock: true, unitPrice: 125.5, quantity: 2 };
const laptop = { name: 'Laptop', inStock: true, unitPrice: 920.99, quantity: 1 };
const cables = { name: 'HDMI Cables', inStock: true, unitPrice: 12.99, quantity: 4 };
const headphones = { name: 'Headphones', inStock: false, unitPrice: 40, quantity: 1 };
const order = [keyboard, laptop, cables, headphones];
const coupon = 0.2;
const inStock = (product) => product.inStock;
const productPrice = (product) => product.unitPrice * product.quantity;
const applyCoupon = (customerCoupon) => (total) => total - (total * customerCoupon);
const sum = (total, price) => total + price;
const result = order
|> i => i.filter(inStock)
|> i => i.map(productPrice)
|> i => i.reduce(sum, 0)
|> applyCoupon(coupon) // partial function application
|> t => t.toFixed(2);
console.log(`Order total is $${result} by applying a 20% discount`);
// prints: Order total is $979.16 by applying a 20% discount
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.