Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
I am wanting to print the properties of this class, all is well until I see that when I show the AddPet() method in the console

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);

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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 :)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!