it shows me that I have the value 'perro' that I pass to the object and a value that is undefined when I did not requested it in any other side, what is the cause of this error?
class Usuario {
constructor(nombre, apellido, libros, mascotas) {
this.nombre = nombre;
this.apellido = apellido;
this.libros = libros;
this.mascotas = mascotas;
}
getFullName() {
console.log(`${this.nombre} ${this.apellido}`);
}
addMascota(mascota) {
this.mascotas.push(mascota);
}
countMascotas() {
return this.mascotas.length;
}
addBook(nombre, autor) {
this.libros.push(nombre, autor);
}
static definicion() {
console.log("Una persona es un ser humano!");
}
}
const usuario = new Usuario(
"John",
"Doe", [{
nombre: "El señor de los anillos",
autor: "J.R.R. Tolkien"
}], ['perro']
);
usuario.getFullName();
usuario.addMascota();
usuario.countMascotas();
console.log(usuario);
You don't seem to have added an argument into addMascota()
In the initiation you have this.mascotas set to ["perro"],
so without having an argument for addMascota() it will add
undefined to the list, resulting in ["perro", undefined]
hopefully this helped :)