-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
96 lines (89 loc) · 2.92 KB
/
script.js
File metadata and controls
96 lines (89 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const fetchBook=()=>{
fetch('https://api.itbook.store/1.0/new')
.then(response=>response.json())
.then(data=>showBook(data))
}
const showBook=(data)=>{
// console.log(data);
const bookContainer=document.getElementById('display-book');
for (let i = 7; i <=12; i++) {
const book = data.books[i];
console.log(book);
const div=document.createElement('div');
div.classList.add('col');
const price = book.price.slice(1);
console.log(price);
div.innerHTML=`
<div class="cards p-3">
<img src="${book.image}" class="img-thumbnail" alt="...">
<div class="card-body">
<h5 class="card-title">${book.title}</h5>
<div class="card-info">
<p class="card-text">${book.price}</p>
<button onclick="countBook(${price})"><i class="fas fa-cart-arrow-down"></i></button>
</div>
</div>
</div>
`;
bookContainer.appendChild(div);
};
}
fetchBook();
let count=0;
const countBook=(price)=>{
count++;
const cart=document.getElementById('total-product').innerText = count;
updatePrice(price);
total();
}
const updatePrice=(price)=>{
const oldPrice = parseFloat(document.getElementById('price').innerText);
const newPrice = oldPrice + price;
document.getElementById('price').innerText = newPrice.toFixed(2);
deliveryCharge(newPrice);
}
const deliveryCharge=(price)=>{
let charge =0;
if(price<500){
charge = 0;
}
else if(price>=500 && price<800){
charge = 100;
}
else if(price>=800 && price<1000){
charge = 150;
} else{
charge = 200;
}
document.getElementById('delivery-charge').innerText=charge;
document.getElementById('shipping-charge').innerText=charge;
subtotal = price+charge+charge;
document.getElementById('subtotal').innerText=subtotal.toFixed(2);
tax(subtotal);
}
const tax=(subtotal)=>{
const tax = subtotal*0.15;
document.getElementById('tax').innerText=tax.toFixed(2);
subtotal+=tax;
total(subtotal);
}
const total=(total)=>{
document.getElementById('total-price').innerText=total.toFixed(2);
}
const greetings=()=>{
const totalPrice = parseFloat(document.getElementById('total-price').innerText);
if(totalPrice==0){
swal("Please add some books at cart", "", "error");
}
else{
swal("Thanks for your Order","Your total price is: "+totalPrice, "success");
count=0;
document.getElementById('total-product').innerText=count;
document.getElementById('price').innerText=0;
document.getElementById('delivery-charge').innerText=0;
document.getElementById('shipping-charge').innerText=0;
document.getElementById('subtotal').innerText=0;
document.getElementById('tax').innerText=0;
document.getElementById('total-price').innerText=0;
}
}