JSFiddle - React, Tailwind, and code Playground

by Vitaliy

JavaScript

class TV{
  constructor(){
    this._power = false;
    this._channel = 1;
  }
  
  power(){
    if(this._power){
      this._power = false;
      console.log('TV is off');
    }else{
      this._power = true;
      console.log(`TV is on.The channel is #${this._channel}`);
    }
  }

  switchChannelTo(number){
    this._checkNumber(number);
    this._channel = number;
    console.log(`Switch to channel #${this._channel}`);
  }

  switchChannelForward(){
    this._channel = this._channel < 99 ? ++this._channel : 1;
    console.log(`Switch forward to channel #${this._channel}`);
  }
  
  switchChannelBackward(){
    this._channel = this._channel > 1 ? --this._channel : 99;
    console.log(`Switch backward to channel #${this._channel}`);
  }

  _checkNumber(number){
    if(isNaN(number) || number < 1 || number > 99 || number % parseInt(number) > 0){
        throw new Error('Invalid channel number');
    }
  }

}

const tv1 = new TV();
tv1.power();
tv1.switchChannelForward();
tv1.switchChannelForward();
tv1.switchChannelForward();
tv1.switchChannelBackward();
tv1.switchChannelBackward();
tv1.switchChannelTo(57);
tv1.power();
tv1.power();