-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (44 loc) · 1.86 KB
/
index.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
const btn = document.getElementById('BotonEnviar')
const peliculasPorMes = document.getElementById('PeliculasPorMes')
const costoPorBoleto = document.getElementById('CostoPorBoleto')
const mensaje = document.getElementById('Mensaje')
btn.onclick = () => {
mensaje.innerText = ""
const formulario = document.getElementById('formulario')
ajustarAtributoRequerido();
if (!formulario.reportValidity()) return
const costoCine = peliculasPorMes.value * costoPorBoleto.value
const costoPlataformas = obtenerTotalPlataformas()
if (costoPlataformas === 0) {
mensaje.innerText = "Elige una o mas plataformas para continuar"
return
}
if (costoCine === costoPlataformas) {
mensaje.innerText = "Gastas lo mismo en ambos lados, así que tu decide 😉"
} else if (costoCine > costoPlataformas) {
mensaje.innerText = 'Definitivo, prepara tu sofá y disfruta el cine desde tu sala 📺'
} else {
mensaje.innerText = 'Corre al cine que ya empieza la función 🍿'
}
}
function obtenerTotalPlataformas() {
const plataformasSeleccionadas = document.querySelectorAll('section input[type="checkbox"]')
let total = 0
plataformasSeleccionadas.forEach((elemento) => {
if(elemento.checked) {
total += Number(document.getElementById('Costo' + elemento.id).value)
}
})
return total
}
function ajustarAtributoRequerido() {
const plataformasSeleccionadas = document.querySelectorAll('section input[type="checkbox"]')
plataformasSeleccionadas.forEach((elemento) => {
const elementoActual = document.getElementById('Costo' + elemento.id)
if(elemento.checked) {
elementoActual.setAttribute('required', true)
} else {
elementoActual.removeAttribute('required')
}
})
}