@kikosko

Как создать ошибку “typeError”, чтобы избежать попадания в вечный цикл?

Мне нужно создать «typeError» в методе «kick», но когда метод выполняется, я попадаю в вечный цикл. Как это исправить?
function Weapon(name, damage) {
    this.name = name;
    this.damage = damage;
    this.getDamage = function () {
        return this.damage;
    };
    Object.defineProperty(this, 'toString', {
        value: function () {
            return name + 'damage: ' + this.getDamage() + " points";
        }
    });
}
var bow = new Weapon('Golden bow, ', 20);
var sword = new Weapon('Magic sword, ', 40);

function Unit(maxHealth, basicDamage, evasion, type) {
    this.maxHealth = maxHealth;
    this.currentHeaalth = maxHealth;
    this.basicDamage = basicDamage;
    this.evasion = evasion;
    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 */
    this.isAlive = function() {
        return this.currentHeaalth > 0;
    };
    /* a method that
     shows the level of health*/
    this.getFormattedHealth = function() {
        return this.currentHeaalth + "/" + this.maxHealth + " HP";
    };
    /*method which fills an array with 10 digit of 0s and 1s according
    to evasion and return a random 1 or 0:*/
    this.probability = function() {
        var notRandomNumbers = [],
            maxEvasion = 0;
        if (
            (this.evasion + "").split(".")[0] == 1 &&
            (this.evasion + "").split(".")[1] == 0
        ) {
            maxEvasion = 10;
        } else {
            maxEvasion = (this.evasion + "").split(".")[1];
        }
        for (var i = 0; i < maxEvasion; i++) {
            notRandomNumbers.push(1);
        }
        for (var i = 0; i < 10 - maxEvasion; i++) {
            notRandomNumbers.push(0);
        }
        var idx = Math.floor(Math.random() * notRandomNumbers.length);
        if (idx == notRandomNumbers[idx] ) {
            console.log('miss');
        }else {
            console.log('hit');
        }
        return notRandomNumbers[idx];
    };
        /* The method that defines the weapon created in the constructor "Weapon"*/
     this.setWeapon = function (weapon) {
            if (weapon instanceof Weapon) {
                try {
                    var me = this;
                    me.weapon = weapon;
                    return me;
                    throw new TypeError('incorrect type of weapon');
                } catch (e) {
                    alert(e.message);

                }
            } else {
                throw (e);

            }
        };
    /*a method that returns the base damage of the hero and damage to the
     weapon (if it is set)*/
    this.getDamage = function () {
         return (this.weapon ? this.weapon.getDamage() : 0) + this.basicDamage;
    };
    /* The method of hitting
     the hero for the chosen purpose*/
   this.kick = function (target) {
           if (target instanceof Unit) {
               try {
                   if (this.isAlive()) {
                     target.currentHeaalth = Math.max(0, target.currentHeaalth - this.probability() * this.getDamage());
                      }
                      return this;
                   throw new TypeError('incorrect type of target');
               }catch (e) {
                   alert(e.message);
               }
           }else  {
                throw (e);
           }
        };
    }
    /*method for showing all the characteristics of the hero and changes
     with them*/
    this.toString = function () {
        return "Type - " + this.type + ", is alive - " + this.isAlive() + ", Have weapon - " + this.weapon +
            ", " + this.getFormattedHealth() + ', hero current damage - ' + this.getDamage() + ' points' +
            ", hero evasion - " + this.evasion;
    }

}
function Archer(maxHealth, basicDamage, evasion) {
    Unit.apply(this, arguments);
    this.type = "archer";
}
var archer = new Archer(60, 5, 0.6);
archer.setWeapon(bow);
function Swordsman(maxHealth, basicDamage, evasion) {
    Unit.apply(this, arguments);
    this.type = "swordsman";
}
var swordsman = new Swordsman(100, 10, 0.3);
swordsman.setWeapon(sword);

// cycle until someone dies
while (archer.isAlive() && swordsman.isAlive()) {
    archer.kick(swordsman);
    swordsman.kick(archer);
}

console.log(archer.toString());
console.log(swordsman.toString());
  • Вопрос задан
  • 117 просмотров
Пригласить эксперта
Ваш ответ на вопрос

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

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