Untitled
class Estudiante { constructor(nombre, notas) { this._nombre = ""; this._notas = []; this.nombre = nombre; this.notas = notas; } set nombre(valor) { if (/^[a-zA-ZáéíóúÁÉÍÓÚñÑ ]+$/.test(valor)) { this._nombre = valor; } else { throw window.alert("El nombre solo puede contener letras y espacios."); } } get nombre() { return this._nombre; } set notas(valor) { if ( Array.isArray(valor) && valor.length === 3 && valor.every( (n) => /^[0-9](\.[0-9]+)?$|^10(\.0+)?$/.test(n) && n >= 0 && n <= 10 ) ) { this._notas = valor.map(Number); } else { throw window.alert("Las notas deben ser tres números entre 0 y 10."); } } get notas() { return this._notas; } static calcularPromedio(notas) { let suma = 0; for (let nota of notas) { suma += nota; } return (suma / notas.length).toFixed(2); } obtenerMensaje() { const promedio = Estudiante.calcularPromedio(this.notas); if (promedio >= 7) { return `¡Felicidades ${this.nombre}! 🎉 Promedio: ${promedio}.`; } else if (promedio >= 5) { return `${this.nombre}, aprobaste 👍 Promedio: ${promedio}.`; } else { return `${this.nombre}, repites ❌ Promedio: ${promedio}.`; } } } function calcularPromedio() { const nombre = document.getElementById("nombre").value.trim(); const notas = [ document.getElementById("nota1").value.trim(), document.getElementById("nota2").value.trim(), document.getElementById("nota3").value.trim(), ]; const estudiante = new Estudiante(nombre, notas); document.getElementById("resultado").textContent = estudiante.obtenerMensaje(); }
Leave a Comment