Untitled

mail@pastecode.io avatar
unknown
typescript
2 years ago
1.3 kB
1
Indexable
Never
import products from './products';

const productName: string = 'fanny pack';
let shipping: number = 0;
let taxPercent: number;
let taxTotal: number;
let total: number;
let shippingAddress: string = 'Bohusgatan 14, 113 22 Stockholm';


// const product = products.filter(product => product.name === productName)[0];

let product;

for (let i = 0; i < products.length; i++) {
  if (products[i].name === productName){
    product = products[i];
  }
}

// console.log(product);

if (String(product.preOrder) === 'true') {
  console.log('We will send you a message when the product is on its way.');
}
else {
  console.log('This product is not available for pre order.')
}

if (Number(product.price) >= 25) {
  shipping = 0;
  console.log('This product has free shipping');
}
else if (Number(product.price) < 25) {
  shipping = 5;
  console.log(`This product has a shipping fee of ${shipping} dollars.`)
}

if (shippingAddress.match('New York')) {
  taxPercent = 0.1;
}
else {
  taxPercent = 0.5;
}

taxTotal = Number(product.price) * taxPercent;

total = Number(product.price) + taxTotal + shipping;

console.log(`
Product: ${product.name}
Address: ${shippingAddress}
Price: $${product.price}
Tax: $${taxTotal}
Shipping: $${shipping}
Total: $${total}
`);