Created
May 21, 2022 13:23
-
-
Save elyasha/81a16765451602bc18c7643702338e6a to your computer and use it in GitHub Desktop.
LEARN TYPESCRIPT TypeMart
This file contains 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
import products from './products' | |
let productName = "tote bag"; | |
let shipping: number; | |
let taxPercent: number; | |
let taxTotal: number; | |
let total: number; | |
const shippingAddress = 'SHCES 505 bloco A'; | |
const product = products.find((product) => product.name === productName) | |
console.log(product) | |
if (!product) { | |
console.log("There is no product") | |
} else { | |
if (product.preOrder === 'true') { | |
console.log('We will send you a message when your product is on its way.') | |
} | |
if (Number(product.price) > 25) { | |
shipping = 0 | |
console.log('We provide free shipping for this product.') | |
} else { | |
shipping = 5 | |
} | |
if (shippingAddress.match('New York')) { | |
taxPercent = 0.1; | |
} else { | |
taxPercent = 0.05; | |
} | |
taxTotal = Number(product.price) * taxPercent | |
total = Number(product.price) + taxTotal + shipping | |
console.log(` | |
Product: ${product.name} | |
Address: ${shippingAddress} | |
Price: $${product.price} | |
Tax: $${taxTotal.toFixed(2)} | |
Shipping: $${shipping.toFixed(2)} | |
Total: $${total.toFixed(2)} | |
`); | |
} | |
This file contains 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
const products = [ | |
{ | |
name: 'fanny pack', | |
price: '30', | |
preOrder: 'true', | |
}, | |
{ | |
name: 'beanie', | |
price: '15', | |
preOrder: 'false', | |
}, | |
{ | |
name: 'tote bag', | |
price: '20', | |
preOrder: 'false', | |
}, | |
{ | |
name: 'shirt', | |
price: '20', | |
preOrder: 'true', | |
}, | |
{ | |
name: 'hoodie', | |
price: '43', | |
preOrder: 'true', | |
}, | |
]; | |
export default products; |
This file contains 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
Show hidden characters
{ | |
"compilerOptions": { | |
"target": "es2017", | |
"module": "commonjs", | |
"strictNullChecks": true | |
}, | |
"include": ["**/*.ts"] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment