forked from juandc/platzi-curso-practico-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdescuentos.js
59 lines (45 loc) · 1.34 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
// const precioOriginal = 120;
// const descuento = 18;
function calcularPrecioConDescuento(precio, descuento) {
const porcentajePrecioConDescuento = 100 - descuento;
const precioConDescuento = (precio * porcentajePrecioConDescuento) / 100;
return precioConDescuento;
}
function onClickButtonPriceDiscount() {
const inputPrice = document.getElementById("InputPrice");
const priceValue = inputPrice.value;
const inputCupon = document.getElementById("InputCupon");
const couponValue = inputCupon.value;
const coupons = [
{
name: "cupon mariano",
discount: 30,
},
{
name: "cupon de las madres",
discount: 20,
},
{
name: "cupon buen pastor",
discount: 10,
},
];
const isCouponValueValid = function (coupon) { //atention with the coupon.name
return coupon.name === couponValue;
};
const userCoupon = coupons.find(isCouponValueValid);
if (!userCoupon) {
alert ("El cupón " + couponValue + " no es válido");
} else {
let descuento = userCoupon.discount;
const precioConDescuento = calcularPrecioConDescuento(priceValue, descuento);
const resultP = document.getElementById("ResultP");
resultP.innerText = "El precio con descuento es: $" + precioConDescuento;
}
}
// console.log({
// precioOriginal,
// descuento,
// porcentajePrecioConDescuento,
// precioConDescuento,
// });