-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.js
76 lines (55 loc) · 2.06 KB
/
code.js
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
const caseIncrement = document.getElementById('case-increment') ;
caseIncrement.addEventListener('click' , function(){
productPriceHandler(true , 'case');
});
// for decrement
const caseDecrement = document.getElementById('case-decrement').addEventListener('click' , function () {
productPriceHandler(false , 'case');
})
var phoneIncrement = document.getElementById('phone_increment') ;
phoneIncrement.addEventListener('click', function(){
productPriceHandler(true, 'phone');
})
var phoneDecrement = document.getElementById('phone_decrement') ;
phoneDecrement.addEventListener('click', function(){
productPriceHandler(false , 'phone');
})
// ================================== handler function ================================
// main function
function productPriceHandler(isIncrease, productName) {
let productValue = document.getElementById(`${productName}-value`);
const productCount = parseInt(productValue.value);
let newProductCount = productCount ;
if (isIncrease == false && productCount > 0) {
newProductCount = productCount - 1;
}
if(isIncrease == true){
newProductCount = productCount + 1 ;
}
productValue.value = newProductCount ;
let productTotal = 0 ;
if(productName== 'case'){
productTotal = newProductCount * 59 ;
}
if(productName == 'phone'){
productTotal = newProductCount * 1219 ;
}
document.getElementById(`${productName}TotalPrice`).innerText ="$"+productTotal;
Calculator();
}
// calculate funciton
function Calculator() {
const phoneInput = document.getElementById('phone-value');
const phoneValue = parseInt(phoneInput.value);
const caseInput = document.getElementById('case-value');
const caseValue = parseInt(caseInput.value);
// for sub total
var subTotalPrice = phoneValue * 1219 + caseValue * 59 ;
document.getElementById('sub-total').innerText = '$'+subTotalPrice ;
// for tax
var tax = Math.round(subTotalPrice * 10/100);
document.getElementById('product-tax').innerText = '$'+tax ;
// for totalPrice
var totalPrice = subTotalPrice + tax ;
document.getElementById('totalPrice').innerText = '$'+totalPrice;
}