OOP Game Challenge
Instructions:
- Create a constructor to create a
Playerobject with a name - Player should have a
nameas well as alvlset to 1 by default andpointsset to 0 by default - Create a method on the prototype called
gainXpthat takes in a number from 1-10 and adds it to the playerspoints. If the currentpointsare >= 10 then add 1 to thelvland decrement the points by 10. - Create another prototype method called
describethat displays the players stats (name, lvl, points)
You should be able to use the Plyer object like this:
let player1 = new Player('Bob');
let player2 = new Player('Alice');
player1.gainXp(5);
player2.gainXp(7);
player1.gainXp(3);
player2.gainXp(2);
player1.gainXp(8);
player2.gainXp(4);
console.log(player1.describe()); // Bob is level 2 with 6 experience points
console.log(player2.describe()); // Alice is level 2 with 3 experience points
Click For Solution
function Player(name) {
this.name = name;
this.lvl = 1;
this.points = 0;
}
Player.prototype.gainXp = function (xp) {
this.points += xp;
if (this.points >= 10) {
this.lvl++;
this.points -= 10;
}
};
Player.prototype.describe = function () {
return `${this.name} is level ${this.lvl} with ${this.points} experience points`;
};
let player1 = new Player('Bob');
let player2 = new Player('Alice');
player1.gainXp(5);
player2.gainXp(7);
player1.gainXp(3);
player2.gainXp(2);
player1.gainXp(8);
player2.gainXp(4);
console.log(player1.describe());
console.log(player2.describe());