нужна помощь в исправлении небольшого метода в классе - PullRequest
0 голосов
/ 04 мая 2020

Я не уверен, почему это не работает, logi c мне нравится. Я бы хотел, чтобы более опытный глаз мог дать совет. Это должно вернуть _grit как 1. но он возвращает 0. и если я сделаю gainedToughness(8), _grit вернет 4, и он должен вернуть 3.

class Person {
    constructor(name, age, profession){
        this._name = name;
        this._age = age;
        this._profession = profession;
        this._health = 100;
        this._grit = 0;
    }
    get name() {
        return this._name;
    }
    get age() {
        return this._age;
    }
    get profession() {
        return this._profession;
    }
    gotInjured(damageNum){
        const weaker = this._health -= damageNum;
        return weaker;
    }
    gainedToughness(damageNum){.           <---------------------------
        let grittier = this._grit++;
        let lotGrittier = this._grit += 3;
        return damageNum <= 5 ? grittier : lotGrittier; 
    }
}

const donRickles = new Person('donRickles', 33, 'comedian');

console.log(donRickles.gotInjured(1));
console.log(donRickles.gainedToughness(1));      <-----------


1 Ответ

0 голосов
/ 04 мая 2020

Использование вами ++ и += вызывало ваши проблемы. Поскольку они оба предназначены для переназначения переменной, а это не то, что вам нужно.

class Person {
    constructor(name, age, profession){
        this._name = name;
        this._age = age;
        this._profession = profession;
        this._health = 100;
        this._grit = 0;
    }
    get name() {
        return this._name;
    }
    get age() {
        return this._age;
    }
    get profession() {
        return this._profession;
    }
    gotInjured(damageNum){
        const weaker = this._health -= damageNum;
        return weaker;
    }
    gainedToughness(damageNum){
        let grittier = this._grit + 1;
        let lotGrittier = this._grit + 3;
        return damageNum <= 5 ? grittier : lotGrittier; 
    }
}

const donRickles = new Person('donRickles', 33, 'comedian');

console.log(donRickles.gotInjured(1));
console.log(donRickles.gainedToughness(1));
console.log(donRickles.gainedToughness(8));

Если вы хотите обновить значения своих свойств, а также вернуть новое значение, сделайте следующее:

class Person {
    constructor(name, age, profession){
        this._name = name;
        this._age = age;
        this._profession = profession;
        this._health = 100;
        this._grit = 0;
    }
    get name() {
        return this._name;
    }
    get age() {
        return this._age;
    }
    get profession() {
        return this._profession;
    }
    gotInjured(damageNum){
        const weaker = this._health - damageNum;
        this._health = weaker;
        return this._health;
    }
    gainedToughness(damageNum){
        let grittier = this._grit + 1;
        let lotGrittier = this._grit + 3;
        this._grit = damageNum <= 5 ? grittier : lotGrittier; 
        return this._grit;
    }
}

const donRickles = new Person('donRickles', 33, 'comedian');

console.log(donRickles._health)
console.log(donRickles.gotInjured(1));
console.log(donRickles._health)

console.log(donRickles._grit)
console.log(donRickles.gainedToughness(1));
console.log(donRickles._grit)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...