Last active
June 22, 2017 10:07
-
-
Save rajaraodv/38fbaed70f9c5d701230c15232215b89 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//imperative | |
//Returns error or price including tax | |
const tax = (tax, price) => { | |
if (!_.isNumber(price)) return new Error("Price must be numeric"); | |
return price + (tax * price); | |
}; | |
//Returns error or price indluding discount | |
const discount = (dis, price) => { | |
if (!_.isNumber(price)) return (new Error("Price must be numeric")); | |
if (price < 10) return new Error("discount cant be applied for items priced below 10"); | |
return price - (price * dis); | |
}; | |
const isError = (e) => e && e.name == 'Error'; | |
const getItemPrice = (item) => item.price; | |
//shows total price after tax and discount. Need to handle multiple errors. | |
const showTotalPrice = (item, taxPerc, disount) => { | |
let price = getItemPrice(item); | |
let result = tax(taxPerc, price); | |
if (isError(result)) { | |
return console.log('Error: ' + result.message); | |
} | |
result = discount(discount, result); | |
if (isError(result)) { | |
return console.log('Error: ' + result.message); | |
} | |
//display result | |
console.log('Total Price: ' + result); | |
} | |
let tShirt = { name: 't-shirt', price: 11 }; | |
let pant = { name: 't-shirt', price: '10 dollars' }; | |
let chips = { name: 't-shirt', price: 5 }; //less than 10 dollars error | |
showTotalPrice(tShirt) // Total Is: 9.075 | |
showTotalPrice(pant) // Error: Price must be numeric | |
showTotalPrice(chips) //Error: discount cant be applied for items priced below 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that you call
showTotalPrice
with a single parameter, where it expects 3(item, taxPerc, disount)