Compare commits
1 Commits
solucion-e
...
solucion-e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3016e835c4 |
@@ -1 +1,35 @@
|
||||
// Agregar aquí el código javascript
|
||||
const btnSumar = document.querySelector("#sumar");
|
||||
const btnRestar = document.querySelector("#restar");
|
||||
|
||||
function actualizarVista(valor) {
|
||||
const MIN = 0;
|
||||
const MAX = 10;
|
||||
const parrafo = document.querySelector("#contador");
|
||||
parrafo.textContent = valor;
|
||||
|
||||
// Habilitamos o deshabilitamos los botones según el valor actual.
|
||||
// Cuando disabled es true, el botón no responde a clics.
|
||||
btnSumar.disabled = valor === MAX;
|
||||
btnRestar.disabled = valor === MIN;
|
||||
}
|
||||
|
||||
btnSumar.addEventListener("click", () => {
|
||||
// Obtenermos el valor actual
|
||||
let valor = parseInt(document.querySelector('#contador').textContent);
|
||||
// Sumamos 1
|
||||
valor++;
|
||||
// y actualizamos la vista
|
||||
actualizarVista(valor);
|
||||
});
|
||||
|
||||
btnRestar.addEventListener("click", () => {
|
||||
// Obtenermos el valor actual
|
||||
let valor = parseInt(document.querySelector('#contador').textContent);
|
||||
// Restamos 1
|
||||
valor--;
|
||||
// y actualizamos la vista
|
||||
actualizarVista(valor);
|
||||
});
|
||||
|
||||
// Llamamos a actualizarVista al inicio para establecer el estado inicial en 5.
|
||||
actualizarVista(5);
|
||||
|
||||
@@ -8,6 +8,11 @@
|
||||
</head>
|
||||
<body>
|
||||
<h1>Ejercicio 4</h1>
|
||||
<p>
|
||||
<button id="restar">Restar</button>
|
||||
<span id="contador">0</span>
|
||||
<button id="sumar">Sumar</button>
|
||||
</p>
|
||||
|
||||
<script src="ejercicio4.js"></script>
|
||||
</body>
|
||||
|
||||
@@ -1,72 +1 @@
|
||||
const productos = [
|
||||
{ nombre: "Teclado", precio: 8500 },
|
||||
{ nombre: "Mouse", precio: 4200 },
|
||||
{ nombre: "Monitor", precio: 62000 },
|
||||
{ nombre: "Auriculares", precio: 11000 },
|
||||
{ nombre: "Webcam", precio: 15500 }
|
||||
];
|
||||
|
||||
// El carrito es un array de objetos { nombre, precio, cantidad }, que
|
||||
// inicializamos vacío fuera de las funciones (para que sea global):
|
||||
const carrito = [];
|
||||
|
||||
const listaProductos = document.querySelector("#productos");
|
||||
|
||||
function actualizarCarrito() {
|
||||
const totalParrafo = document.querySelector("#total");
|
||||
const listaCarrito = document.querySelector("#carrito");
|
||||
listaCarrito.innerHTML = "";
|
||||
|
||||
// Calculamos el total mientras construimos la lista.
|
||||
let total = 0;
|
||||
|
||||
for (const item of carrito) {
|
||||
const subtotal = item.precio * item.cantidad;
|
||||
total += subtotal;
|
||||
|
||||
const li = document.createElement("li");
|
||||
li.textContent = `${item.nombre} x${item.cantidad} — $${subtotal.toLocaleString()}`;
|
||||
listaCarrito.appendChild(li);
|
||||
}
|
||||
|
||||
totalParrafo.textContent = `Total: $${total.toLocaleString()}`;
|
||||
}
|
||||
|
||||
function agregarAlCarrito(producto) {
|
||||
// Buscamos si el producto ya está en el carrito.
|
||||
const itemExistente = carrito.find(item => item.nombre === producto.nombre);
|
||||
|
||||
if (itemExistente) {
|
||||
// Si ya existe, solo incrementamos la cantidad.
|
||||
itemExistente.cantidad++;
|
||||
} else {
|
||||
// Si no existe, lo agregamos manualmente con cantidad 1,
|
||||
// copiando las propiedades una por una.
|
||||
carrito.push({
|
||||
nombre: producto.nombre,
|
||||
precio: producto.precio,
|
||||
cantidad: 1
|
||||
});
|
||||
}
|
||||
|
||||
actualizarCarrito();
|
||||
}
|
||||
|
||||
// Construimos la lista de productos con un botón por cada uno.
|
||||
for (const producto of productos) {
|
||||
const listaProductos = document.querySelector("#productos");
|
||||
const card = document.createElement("div");
|
||||
card.className = "product-card";
|
||||
card.innerHTML = `
|
||||
<div>
|
||||
<p class="product-name">${producto.nombre}</p>
|
||||
<p class="product-price">$${producto.precio.toLocaleString()}</p>
|
||||
</div>
|
||||
<button class="btn-add">+ Agregar</button>
|
||||
`;
|
||||
card.querySelector(".btn-add").addEventListener("click", () => agregarAlCarrito(producto));
|
||||
listaProductos.appendChild(card);
|
||||
}
|
||||
|
||||
actualizarCarrito();
|
||||
|
||||
// Agregar aquí el código javascript
|
||||
|
||||
@@ -1,215 +1,2 @@
|
||||
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
|
||||
|
||||
body {
|
||||
font-family: system-ui, sans-serif;
|
||||
background: #f5f4f0;
|
||||
color: #1a1a1a;
|
||||
min-height: 100vh;
|
||||
padding: 2rem;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 1.1rem;
|
||||
font-weight: 500;
|
||||
color: #555;
|
||||
margin-bottom: 1.5rem;
|
||||
}
|
||||
|
||||
.layout {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 1fr;
|
||||
gap: 1.5rem;
|
||||
max-width: 820px;
|
||||
}
|
||||
|
||||
.section-label {
|
||||
font-size: 0.7rem;
|
||||
font-weight: 500;
|
||||
color: #888;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.06em;
|
||||
margin-bottom: 0.75rem;
|
||||
}
|
||||
|
||||
/* ── Productos ── */
|
||||
|
||||
.product-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.product-card {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 10px 14px;
|
||||
background: #fff;
|
||||
border: 0.5px solid #ddd;
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
.product-name {
|
||||
font-size: 0.875rem;
|
||||
font-weight: 500;
|
||||
color: #1a1a1a;
|
||||
}
|
||||
|
||||
.product-price {
|
||||
font-size: 0.75rem;
|
||||
color: #888;
|
||||
margin-top: 2px;
|
||||
}
|
||||
|
||||
.btn-add {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
padding: 6px 12px;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 500;
|
||||
background: transparent;
|
||||
border: 0.5px solid #ccc;
|
||||
border-radius: 6px;
|
||||
color: #1a1a1a;
|
||||
cursor: pointer;
|
||||
transition: background 0.15s;
|
||||
}
|
||||
|
||||
.btn-add:hover { background: #f0efeb; }
|
||||
|
||||
/* ── Carrito ── */
|
||||
|
||||
.cart-panel {
|
||||
background: #edecea;
|
||||
border: 0.5px solid #ddd;
|
||||
border-radius: 12px;
|
||||
padding: 1rem 1.25rem;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.cart-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
.badge {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
min-width: 18px;
|
||||
height: 18px;
|
||||
padding: 0 5px;
|
||||
background: #fde8e8;
|
||||
color: #a32d2d;
|
||||
border-radius: 9px;
|
||||
font-size: 0.625rem;
|
||||
font-weight: 500;
|
||||
margin-left: 4px;
|
||||
}
|
||||
|
||||
.cart-empty {
|
||||
font-size: 0.8rem;
|
||||
color: #aaa;
|
||||
text-align: center;
|
||||
padding: 1.5rem 0;
|
||||
}
|
||||
|
||||
.cart-items {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.cart-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 8px 10px;
|
||||
background: #fff;
|
||||
border: 0.5px solid #ddd;
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
.cart-item-name {
|
||||
font-size: 0.8rem;
|
||||
font-weight: 500;
|
||||
color: #1a1a1a;
|
||||
}
|
||||
|
||||
.cart-item-sub {
|
||||
font-size: 0.7rem;
|
||||
color: #888;
|
||||
margin-top: 2px;
|
||||
}
|
||||
|
||||
.cart-item-right {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.cart-item-total {
|
||||
font-size: 0.8rem;
|
||||
font-weight: 500;
|
||||
color: #1a1a1a;
|
||||
min-width: 64px;
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
.qty-controls {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.qty-btn {
|
||||
width: 22px;
|
||||
height: 22px;
|
||||
border: 0.5px solid #ccc;
|
||||
border-radius: 4px;
|
||||
background: transparent;
|
||||
color: #1a1a1a;
|
||||
cursor: pointer;
|
||||
font-size: 0.875rem;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
.qty-btn:hover { background: #f0efeb; }
|
||||
|
||||
.qty-val {
|
||||
font-size: 0.8rem;
|
||||
font-weight: 500;
|
||||
min-width: 16px;
|
||||
text-align: center;
|
||||
color: #1a1a1a;
|
||||
}
|
||||
|
||||
.divider {
|
||||
border: none;
|
||||
border-top: 0.5px solid #ccc;
|
||||
}
|
||||
|
||||
.cart-total {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: baseline;
|
||||
}
|
||||
|
||||
.cart-total-label {
|
||||
font-size: 0.8rem;
|
||||
color: #888;
|
||||
}
|
||||
|
||||
.cart-total-amount {
|
||||
font-size: 1.25rem;
|
||||
font-weight: 500;
|
||||
color: #1a1a1a;
|
||||
}
|
||||
/* Agregar el código CSS necesario para el ejercicio */
|
||||
|
||||
|
||||
@@ -1,39 +1,14 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="es">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Ejercicio 8 — Carrito de compras</title>
|
||||
<style></style>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width">
|
||||
<title>Ejercicio 8</title>
|
||||
<link rel="stylesheet" href="estilo.css">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h1>Ejercicio 8 — Carrito de compras</h1>
|
||||
|
||||
<div class="layout">
|
||||
<div>
|
||||
<p class="section-label">Productos</p>
|
||||
<div class="product-list" id="productos"></div>
|
||||
</div>
|
||||
|
||||
<div class="cart-panel">
|
||||
<div class="cart-header">
|
||||
<p class="section-label" style="margin:0">
|
||||
Carrito
|
||||
</p>
|
||||
</div>
|
||||
<div class="cart-items" id="carrito">
|
||||
<p class="cart-empty" id="empty-msg">El carrito está vacío.</p>
|
||||
</div>
|
||||
<hr class="divider">
|
||||
<div class="cart-total">
|
||||
<span class="cart-total-label">Total</span>
|
||||
<span class="cart-total-amount" id="total">$0</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Ejercicio 8</h1>
|
||||
|
||||
<script src="ejercicio8.js"></script>
|
||||
</body>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user