Untitled
Js 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 new Error("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 new Error("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}.`; } } }
Leave a Comment