Lo hice de dos maneras:
1. array de Objetos:
let promotion = [
{ 'Platzi Vacation': 15 },
{ 'Platzi Weekend': 30 },
{ 'Platzi New Year': 45 },
];
/**Promotion code */
btn.addEventListener('click', () => {
if (!input1.value || !input2.value) {
p.innerText = 'Please, insert values for both price and promotion code!';
} else {
if (promotion.flatMap(item => Object.keys(item)).includes(input2.value)) {
let discount = (+input1.value * (100 - promotion.find(item => item[input2.value])[input2.value])) / 100;
p.innerText = `You get a ${promotion.find(item => item[input2.value])[input2.value]}% off. Price with discount: $${discount}`;
} else { p.innerText = 'There is no promotion with this code.'; }
}
});
2. Objetos:
let promotion = {
'Platzi Vacation': 15,
'Platzi Weekend': 30,
'Platzi New Year': 45,
};
/**Promotion code */
btn.addEventListener('click', () => {
if (!input1.value || !input2.value) {
p.innerText = 'Please, insert values for both price and promotion code!';
} else {
if (Object.keys(promotion).includes(input2.value)) {
let discount = (+input1.value * (100 - promotion[input2.value])) / 100;
p.innerText = `You get a ${promotion[input2.value]}% off. Price with discount: $${discount}`;
} else { p.innerText = 'There is no promotion with this code.'; }
}
});
Los aportes, preguntas y respuestas son vitales para aprender en comunidad. RegÃstrate o inicia sesión para participar.