how do you write methods to change the x and y positions in a constructible function?

Here you go, you just need to delete unreferenced variable console.log(value) and update the property x and y in there setters

<script>

var Character = function(name, xPos, yPos) {
  this.name = name;
  this.xPos = xPos;
  this.yPos = yPos;

  // the changedXPos and changedYPos functions are part of the prototype itself
  // therefore they should be inside the prototype's outer braces { }
  this.changedXPos = function(value) {
    this.xPos = value;
  }
  
  this.changedYPos = function(value) {
    this.yPos = value;
  }
  this.details = function() {
    return "The person's name is: " + this.name + " and their xPos is: " + this.xPos + "and their yPos is: " + this.yPos
  };
  Character.prototype.toString = function toString() {
    var info = "Name: " + this.name + " xPos is " + this.xPos + " and yPos is " + this.yPos;
    return info;
  };
} // end of prototype

// create the 3 character objects here
// output the name and currect position of each character

var john = new Character("John", 10, 30);

console.log(john.toString(john));

var joe = new Character("Joe", 40, 20);

console.log(joe.toString(joe));

var doe = new Character("Doe", 50, 10);

console.log(doe.toString(doe));

// change the position of each character
// output the new position of each character

// now call the changedXPos and changedYPos functions
john.changedXPos(20);
john.changedYPos(50);
// print the new position to the console
console.log("new position for john: " + john.toString(john));
document.write("new position for john: " + john.toString(john));
</script>

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top