Задать вопрос
  • Как создать свой собственный метод?

    @vit_grey Автор вопроса
    прошу извинить за мою наглость, но Вы могли бы расписать пример полностью, я в прототипах слабо розбираюсь, изучаю.
  • Как применить условие выполнения к методам класса?

    @vit_grey Автор вопроса
    оно не работает(
    'use strict'

    class Tamagotchi {

    constructor(name){
    this.name = name;
    this.health = 100;
    }

    healthLo() {
    this.health -= 20;
    }

    healthHi() {
    this.health += 20;
    }

    hello() {
    console.log('HI! My name is ' + this.name + '! Play with me!');
    }

    eat() {
    let eatHamster = confirm("I want to eat...");
    if(eatHamster) {
    this.healthHi();
    console.log(this.name + " eating now...plus to health 20%! " + "current health value:" + this.health );
    } else {
    this.healthLo();
    console.log('Time to drink...' + "current health value:" + this.health);
    }
    }

    drink() {
    let drinkHamster = confirm("I want to drink...");
    if(drinkHamster) {
    this.healthHi();
    console.log(this.name + " drinking now...plus to health 20% " + "current health value:" + this.health );
    } else {
    this.healthLo();
    console.log('Time to sleep...' + "current health value:" + this.health);
    }
    }

    sleep() {
    let sleepHamster = confirm("I want to sleep...");
    if(sleepHamster) {
    this.healthHi();
    console.log(this.name + " sleeping now...plus to health 20% " + "current health value:" + this.health );
    } else {
    this.healthLo();
    console.log('Time to play...' + "current health value:" + this.health);
    }
    }

    play() {
    let playHamster = confirm("I want to play...");
    if(playHamster) {
    this.healthHi();
    console.log(this.name + " playing now...plus to health 20% " + "current health value:" + this.health );
    } else {
    this.healthLo();
    if(this.health > 20) {
    console.log("I like you!" + "current health value:" + this.health);
    } else {
    console.log("I want to died...help me!" + "current health value:" + this.health);
    }
    }
    }


    sick() {
    this.healthLo();
    console.log('pills dont help!!!' + "current health value:" + this.health);
    }

    died() {
    console.log('Sorry! Your ' + this.name + ' is died!!!' + "current health value:" + this.health);
    }

    }

    let name = prompt('Enter please name: ','name');

    let hamster = new Tamagotchi(name);

    if (hamster) {
    hamster.hello();
    }

    if (hamster.health > 80 || hamster.health < 100) {
    hamster.eat();
    }

    if (hamster.health > 60 || hamster.health < 80) {
    hamster.sleep();
    }

    if (hamster.health >= 100) {
    hamster.play();
    }

    if (hamster.health > 40 || hamster.health < 60) {
    hamster.drink();
    }

    if (hamster.health > 20 || hamster.health < 40) {
    hamster.sick();
    }

    if (hamster.health < 20) {
    hamster.died();
    }