-
Notifications
You must be signed in to change notification settings - Fork 0
/
descuentos.js
103 lines (83 loc) · 2.57 KB
/
descuentos.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const coupons = [
{
name: "J4CK5I70M4SN44",
discount: 15,
},
{
name: "3S7R3N473",
discount: 30,
},
{
name: "H45G4N4D0",
discount: 25,
},
{
name: "54N5UNGS51",
discount: 45,
}
/*"J4CK5I70M4SN44",
"3S7R3N473",
"H45G4N4D0",
"54N5UNGS51"*/
];
function calcularPrecioConDescuento(precio, descuento){
const porcentajePrecioConDescuento = 100 - descuento;
const precioConDescuento = (precio * porcentajePrecioConDescuento)/100;
return precioConDescuento;
};
function onClickButtonPriceDiscount(){
const inputPrice = document.getElementById("inputPrice");
const valuePrice = inputPrice.value;
const inputDiscount = document.getElementById("inputDiscount");
const valueDiscount = inputDiscount.value;
let descuento;
const precioConDescuento = calcularPrecioConDescuento(valuePrice, valueDiscount);
const resultPriceDiscount = document.getElementById("resultPriceDiscount")
resultPriceDiscount.innerText = `El precio con descuento son: S/${precioConDescuento}`
console.log(resultPriceDiscount);
}
function onClickButtonPriceCoupon(){
const inputPrice = document.getElementById("inputPrice");
const valuePrice = inputPrice.value;
const inputCoupon = document.getElementById("inputCoupon");
const valueCoupon = inputCoupon.value;
let descuento;
const isCouponValueValid = function (coupon) {
return coupon.name === valueCoupon;
};
const userCoupon = coupons.find(isCouponValueValid);
if (!userCoupon) {
alert("El cupón " + valueCoupon + " no es válido");
}else{
const descuento = userCoupon.discount;
const precioConDescuento = calcularPrecioConDescuento(valuePrice, descuento);
const resultPriceCoupon = document.getElementById("resultPriceCoupon")
resultPriceCoupon.innerText = `El precio con cupón son: S/${precioConDescuento}`
console.log(resultPriceCoupon);
}
/*if (!coupons.includes(valueCoupon)) {
alert("El cupón " + valueCoupon + "no es válido");
} else if(valueCoupon === "J4CK5I70M4SN44") {
descuento = 15;
} else if (valueCoupon === "3S7R3N473") {
descuento = 30;
} else if (valueCoupon === "H45G4N4D0") {
descuento = 25;
} else if (valueCoupon === "54N5UNGS51") {
descuento = 45;
}*/
/*switch (valueCoupon) {
case coupon[0]:
descuento = 15; // "J4CK5I70M4SN44",
break;
case coupon[1]:
descuento = 30; // "3S7R3N473",
break;
case coupon[2]:
descuento = 25; // "H45G4N4D0",
break;
case coupon[3]:
descuento = 45; // "54N5UNGS51"
break;
}*/
};