When creating an object, I store it in memory or in an array variable, for example. If I want to add a new object with an IDENTICAL field that I previously created in the first object, I would have to give me an error: "El nombre que deseas introducir YA existe".
I have the function existeDisco
, to which I must pass it by parameter / argument, a name (the name of the disk):
//Clase Tienda. Constructor sin parametros.
var Tienda = function(){
//Creamos un array de discos.
this.numTotalDiscos = new Array();
//Devuelve verdadero si existe el disco cuyo titulo coincide con el que se pasa.
this.existeDisco = function(tituloRecibido){
var existe = false;
for(var i=0; i<numTotalDiscos.length;i++){
var titulo = this.numTotalDiscos[i].nombre;
if(titulo==tituloRecibido)
existe = true;
break;
}
return existe;
}
}
When creating the object, I save it using the function addDisco(disco);
function to which the disk passed ( objeto
).
Before saving it I think you should check if the name of the disk I want to add exists in some previous object.
/*Creo un objeto/instancia de la clase Tienda.*/
var miTienda[] = new Tienda();
/* Registro un evento para cuando termine de cargarse el documento se cargen los eventos necesarios.
*/
window.addEventListener('load', iniciar, true);
//Función que se ejecuta tras cargarse el documento y registra el resto de eventos.
function iniciar(){
//Evento para validar que los campos estén con información correcta y NO estén vacíos.
document.getElementById('guardar').addEventListener('click', validarCampos, false);
}
/******************************************************************************
**************** FUNCIONES PARA LA COMPROBACIÓN DE LOS CAMPOS ****************
******************************************************************************/
function validarCampos(evento){
//Valido los diferentes apartados del formulario. Si todo está OK...
if (validarCamposText(this) && comprobarNombre() && comprobarCantante() && comprobarAnio() && comprobarEstanteria() && confirm("¿Desea enviar el disco?")){
var nombre = document.getElementById('nombre').value;
var cantante = document.getElementById('cantante').value;
var anio = document.getElementById('anio').value;
var tipo = document.getElementById('tipo').value;
var estanteria = document.getElementById('estanteria').value;
var tipo = document.getElementById("tipo").value;
var radio_prestado = document.getElementsByName('prestado');
var prestado;
for (var i=0; i<radio_prestado.length; i++) {
//Así cogemos el valor del radio que esté checked.
if (radio_prestado[i].checked) {
prestado = radio_prestado[i].value;
break;
}
}
var disco = new Disco(nombre, cantante, anio, tipo, estanteria, prestado);
/////////Antes de agregarlo debería hacer la comprobacion no???////
miTienda.addDisco(disco);
document.getElementById("errores").innerHTML="El disco "+nombre+" se ha insertado correctamente";
return true;
}else{
//Cancelar el evento por defecto del envío del formulario.
evento.preventDefault();
return false;
}
}
The doubt is in the comment that I have inside the code.