How to write a function that takes an argument and adds it to an array? [closed]

You have a few things wrong:

  1. listPhotos.push should be this.listPhotos.push
  2. You should be pushing the variable x and not the string x
  3. When logging you want this.listPhotos and not x.listPhotos
function Album() {
  this.listPhotos = ["bee", "ladybug", "caterpillar", "ant"];
  this.addPhoto = function(x) {
    this.listPhotos.push(x);
    console.log(this.listPhotos);
  }
}

let a = new Album()
a.addPhoto('123')

Leave a Comment