@kikosko

Как сделать проверку по свойству конструктора?

Я хочу что бы метод "isAlive" класса "Team" выводил false, если "currentHeaalth" = 0 у всех юнитов в массиве "this.member". У меня отображается "undefined"

function Unit(maxHealth, basicDamage, type) {
    this.maxHealth = maxHealth;
    this.currentHeaalth = maxHealth;
    this.basicDamage = basicDamage;
    this.type = type;
}
/*method for showing the status of life, true if the "health" is greater
 than 0 and false if equal to or lower */
Unit.prototype.isAlive = function () {
    return this.currentHeaalth > 0;
};
/* a method that
 shows the level of health*/
Unit.prototype.getFormattedHealth = function () {
    return this.currentHeaalth + "/" + this.maxHealth + " HP";
};

/*a method that returns the base damage of the hero and damage to the
 weapon (if it is set)*/
Unit.prototype.getDamage = function () {
    return this.basicDamage;
};
/* The method of hitting
 the hero for the chosen purpose*/
Unit.prototype.kick = function (target) {
    if (this.isAlive()) {
        target.currentHeaalth = Math.max(0, target.currentHeaalth -  this.getDamage());
    }
    return this;
};
/*method for showing all the characteristics of the hero and changes
 with them*/
Unit.prototype.toString = function () {
    return "Type - " + this.type + ", is alive - " + this.isAlive()  +
        ", " + this.getFormattedHealth() + ', hero current damage - ' + this.getDamage() + ' points';
};

function Archer(maxHealth, basicDamage) {
    Unit.apply(this, arguments);
    this.type = "archer";
}
function Swordsman(maxHealth, basicDamage) {
    Unit.apply(this, arguments);
    this.type = "swordsman";
}
function Mage(maxHealth, basicDamage) {
    Unit.apply(this, arguments);
    this.type = "mage";
}
Archer.prototype = Object.create(Unit.prototype);
Swordsman.prototype = Object.create(Unit.prototype);
Mage.prototype = Object.create(Unit.prototype);

var archer = new Archer(0, 5);
var swordsman = new Swordsman(100, 10);
var mage = new Mage(40, 15);

var troll = new Archer(0, 5);
var orc = new Swordsman(0, 10);
var druid = new Mage(0, 15);

function Team(name) {
    this.name = name;
    this.members = [];
}
/*method for adding a new unit with an arbitrary number of units*/
Team.prototype.addMember = function (...members) {
 for (var i=0; i< members.length;i++) {
     this.members.push(members[i]);
    }
}
/*method of life of the team, if all participants have "currentHealth" <0 then this method = "false"*/
Team.prototype.isAlive = function () {
    if(this.members.currentHeaalth < 0) {
        return false
    }
};
/*method to output information about the team*/
Team.prototype.toString = function () {
    var res = "Name of team - " + this.name +  '\n' + "life of a team : " + this.isAlive() + '\n' +"members :\n";
    for (var i=0; i<this.members.length; i++)
        res += this.members[i]+"\n";
    return  res;
};

var team1 =  new Team('Alliance');
team1.addMember(archer,swordsman,mage);

var team2 = new Team('Orcs');
team2.addMember(troll,orc,druid);

console.log(team1.toString());
console.log(team2.toString());
  • Вопрос задан
  • 56 просмотров
Решения вопроса 1
Vlad_IT
@Vlad_IT Куратор тега JavaScript
Front-end разработчик
Там массив, нужно пройтись циклически по массиву и для каждого элемента сделать проверку. Можно не циклом, а every
Team.prototype.isAlive = function () {
   return !this.members.every(function(memb) { // Вернет true, если все элементы == 0. В начале я поставил !, чтобы было false а не true
      return memb.currentHeaalth === 0;
   });
};
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы