ejercicio 8: carrito de compras
This commit is contained in:
@@ -1 +1,61 @@
|
|||||||
// Agregar aquí el código javascript
|
const formulario = document.getElementById('formulario');
|
||||||
|
const nombreInput = document.getElementById('nombre');
|
||||||
|
const edadInput = document.getElementById('edad');
|
||||||
|
const contrasenaInput = document.getElementById('contrasena');
|
||||||
|
|
||||||
|
const errorNombre = document.getElementById('error-nombre');
|
||||||
|
const errorEdad = document.getElementById('error-edad');
|
||||||
|
const errorContrasena = document.getElementById('error-contrasena');
|
||||||
|
const mensajeExito = document.getElementById('mensaje-exito');
|
||||||
|
|
||||||
|
function limpiarErrores() {
|
||||||
|
errorNombre.textContent = '';
|
||||||
|
errorEdad.textContent = '';
|
||||||
|
errorContrasena.textContent = '';
|
||||||
|
mensajeExito.textContent = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
function validarFormulario() {
|
||||||
|
limpiarErrores();
|
||||||
|
|
||||||
|
let esValido = true;
|
||||||
|
const nombre = nombreInput.value.trim();
|
||||||
|
const edad = edadInput.value.trim();
|
||||||
|
const contrasena = contrasenaInput.value;
|
||||||
|
|
||||||
|
if (nombre === '') {
|
||||||
|
errorNombre.textContent = 'El nombre no puede quedar vacío.';
|
||||||
|
esValido = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (edad === '') {
|
||||||
|
errorEdad.textContent = 'La edad no puede quedar vacía.';
|
||||||
|
esValido = false;
|
||||||
|
} else {
|
||||||
|
const edadNumero = Number(edad);
|
||||||
|
if (!Number.isInteger(edadNumero) || edadNumero < 0 || edadNumero > 100) {
|
||||||
|
errorEdad.textContent = 'La edad debe ser un número entero entre 0 y 100.';
|
||||||
|
esValido = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (contrasena === '') {
|
||||||
|
errorContrasena.textContent = 'La contraseña no puede quedar vacía.';
|
||||||
|
esValido = false;
|
||||||
|
} else if (contrasena.length < 8) {
|
||||||
|
errorContrasena.textContent = 'La contraseña debe tener al menos 8 caracteres.';
|
||||||
|
esValido = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return esValido;
|
||||||
|
}
|
||||||
|
|
||||||
|
formulario.addEventListener('submit', (event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
|
if (validarFormulario()) {
|
||||||
|
mensajeExito.textContent = 'Formulario enviado correctamente.';
|
||||||
|
} else {
|
||||||
|
mensajeExito.textContent = '';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|||||||
@@ -1,2 +1,42 @@
|
|||||||
/* Agregar el código CSS necesario para el ejercicio */
|
/* Agregar el código CSS necesario para el ejercicio */
|
||||||
|
|
||||||
|
body {
|
||||||
|
font-family: Arial, sans-serif;
|
||||||
|
margin: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.campo {
|
||||||
|
margin-bottom: 15px;
|
||||||
|
}
|
||||||
|
|
||||||
|
label {
|
||||||
|
display: block;
|
||||||
|
margin-bottom: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
input {
|
||||||
|
display: block;
|
||||||
|
width: 100%;
|
||||||
|
max-width: 320px;
|
||||||
|
padding: 8px;
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
|
||||||
|
button {
|
||||||
|
padding: 10px 16px;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.error {
|
||||||
|
display: block;
|
||||||
|
margin-top: 4px;
|
||||||
|
color: #c0392b;
|
||||||
|
font-size: 0.95rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.exito {
|
||||||
|
margin-top: 16px;
|
||||||
|
color: #2d7a2d;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,13 +2,37 @@
|
|||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<meta name="viewport" content="width=device-width">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>Ejercicio 7</title>
|
<title>Ejercicio 7</title>
|
||||||
<link rel="stylesheet" href="estilo.css">
|
<link rel="stylesheet" href="estilo.css">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Ejercicio 7</h1>
|
<h1>Ejercicio 7</h1>
|
||||||
|
|
||||||
|
<form id="formulario">
|
||||||
|
<div class="campo">
|
||||||
|
<label for="nombre">Nombre:</label>
|
||||||
|
<input id="nombre" name="nombre" type="text">
|
||||||
|
<span class="error" id="error-nombre"></span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="campo">
|
||||||
|
<label for="edad">Edad:</label>
|
||||||
|
<input id="edad" name="edad" type="number" min="0" max="100">
|
||||||
|
<span class="error" id="error-edad"></span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="campo">
|
||||||
|
<label for="contrasena">Contraseña:</label>
|
||||||
|
<input id="contrasena" name="contrasena" type="password">
|
||||||
|
<span class="error" id="error-contrasena"></span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button type="submit">Enviar</button>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<p id="mensaje-exito" class="exito" aria-live="polite"></p>
|
||||||
|
|
||||||
<script src="ejercicio7.js"></script>
|
<script src="ejercicio7.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -1 +1,71 @@
|
|||||||
// Agregar aquí el código javascript
|
const productos = [
|
||||||
|
{ nombre: "Teclado", precio: 8500 },
|
||||||
|
{ nombre: "Mouse", precio: 4200 },
|
||||||
|
{ nombre: "Monitor", precio: 62000 },
|
||||||
|
{ nombre: "Auriculares", precio: 11000 },
|
||||||
|
{ nombre: "Webcam", precio: 15500 }
|
||||||
|
];
|
||||||
|
|
||||||
|
const contenedorProductos = document.querySelector("#productos");
|
||||||
|
const carritoLista = document.querySelector("#carrito");
|
||||||
|
const totalTexto = document.querySelector("#total");
|
||||||
|
|
||||||
|
const carrito = [];
|
||||||
|
|
||||||
|
function mostrarProductos() {
|
||||||
|
for (const producto of productos) {
|
||||||
|
const div = document.createElement("div");
|
||||||
|
|
||||||
|
div.innerHTML = `
|
||||||
|
<span>${producto.nombre} - $${producto.precio}</span>
|
||||||
|
<button>Agregar al carrito</button>
|
||||||
|
`;
|
||||||
|
|
||||||
|
const boton = div.querySelector("button");
|
||||||
|
|
||||||
|
boton.addEventListener("click", () => {
|
||||||
|
agregarAlCarrito(producto);
|
||||||
|
});
|
||||||
|
|
||||||
|
contenedorProductos.appendChild(div);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function agregarAlCarrito(producto) {
|
||||||
|
const existente = carrito.find(
|
||||||
|
item => item.nombre === producto.nombre
|
||||||
|
);
|
||||||
|
|
||||||
|
if (existente) {
|
||||||
|
existente.cantidad++;
|
||||||
|
} else {
|
||||||
|
carrito.push({
|
||||||
|
nombre: producto.nombre,
|
||||||
|
precio: producto.precio,
|
||||||
|
cantidad: 1
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
actualizarCarrito();
|
||||||
|
}
|
||||||
|
|
||||||
|
function actualizarCarrito() {
|
||||||
|
carritoLista.innerHTML = "";
|
||||||
|
|
||||||
|
let total = 0;
|
||||||
|
|
||||||
|
for (const item of carrito) {
|
||||||
|
const li = document.createElement("li");
|
||||||
|
|
||||||
|
li.textContent =
|
||||||
|
`${item.nombre} x${item.cantidad} - $${item.precio * item.cantidad}`;
|
||||||
|
|
||||||
|
carritoLista.appendChild(li);
|
||||||
|
|
||||||
|
total += item.precio * item.cantidad;
|
||||||
|
}
|
||||||
|
|
||||||
|
totalTexto.textContent = `Total: $${total}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
mostrarProductos();
|
||||||
|
|||||||
@@ -8,7 +8,15 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Ejercicio 8</h1>
|
<h1>Ejercicio 8</h1>
|
||||||
|
<h1>Productos</h1>
|
||||||
|
|
||||||
|
<div id="productos"></div>
|
||||||
|
|
||||||
|
<h2>Carrito</h2>
|
||||||
|
|
||||||
|
<ul id="carrito"></ul>
|
||||||
|
|
||||||
|
<p id="total">Total: $0</p>
|
||||||
<script src="ejercicio8.js"></script>
|
<script src="ejercicio8.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user