Advertisement
avr39-ripe

tickerClass_shapeClass

Apr 6th, 2020
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Ticker(tickerId, baseDelay=3000, ticksCount=10)
  2. {
  3.     //let this = {};
  4.     console.log("Ticker works hard with new! :)");
  5.     this.tickerId = tickerId;
  6.     this.delay = baseDelay;
  7.     this.ticksCount = ticksCount;
  8.     this.cnt = 0;
  9.     this.id;
  10.     Ticker.prototype.parentProperty = 42;
  11.     Ticker.prototype.tickerArr = () => {
  12.             console.log(`Ticker #${this.tickerId} ${this.cnt}`);
  13.             (this.cnt++) % 2 ? this.delay *= 2 : this.delay /= 2;
  14.             if (this.cnt == this.ticksCount && this.ticksCount != -1) { clearTimeout(this.id); }
  15.             else { this.id = setTimeout(this.tickerArr, this.delay)};
  16.     };
  17.     Ticker.prototype.armArr = function()
  18.     {
  19.         this.cnt = 0;
  20.         this.id = setTimeout(this.tickerArr, this.delay);
  21.     }
  22.     Ticker.prototype.arm = function()
  23.     {
  24.         this.cnt = 0;
  25.         this.id = setTimeout(function ticker(obj){
  26.             console.log(`Ticker #${obj.tickerId} ${obj.cnt}`);
  27.             (obj.cnt++) % 2 ? obj.delay *= 2 : obj.delay /= 2;
  28.             if (obj.cnt == obj.ticksCount && obj.ticksCount != -1) { clearTimeout(obj.id); }
  29.             else { obj.id = setTimeout(ticker, obj.delay,obj)};
  30.         },this.delay,this);
  31.     };
  32.     Ticker.prototype.disarm = function()
  33.     {
  34.         clearTimeout(this.id);
  35.     };
  36.     Ticker.prototype.set = function(baseDelay=3000, ticksCount=10)
  37.     {
  38.         this.delay = baseDelay;
  39.         this.ticksCount = ticksCount;
  40.     };
  41.    
  42.     Ticker.prototype.get = function()
  43.     {
  44.         console.log(`Ticker #${this.tickerId}\nDelay: ${this.delay}\nTicksCount: ${this.ticksCount}`);
  45.     }
  46.     //Object.setPrototypeOf(this,Ticker.prototype)
  47.     //return this;
  48. };
  49.  
  50. class Ticker
  51. {
  52.     constructor(tickerId, baseDelay=3000, ticksCount=10)
  53.     {
  54.         this.tickerId = tickerId;
  55.         this.delay = baseDelay;
  56.         this.ticksCount = ticksCount;
  57.         this.cnt = 0;
  58.         this.id = 0;
  59.     }
  60.     output()
  61.     {
  62.         console.log(`Ticker #${this.tickerId} ${this.cnt}`);
  63.     }
  64.     ticker()
  65.     {
  66.         this.output();
  67.         (this.cnt++) % 2 ? this.delay *= 2 : this.delay /= 2;
  68.         if (this.cnt == this.ticksCount && this.ticksCount != -1) { clearTimeout(this.id); }
  69.         else { this.id = setTimeout(()=>{this.ticker()}, this.delay)};
  70.     };
  71.  
  72.     arm()
  73.     {
  74.         this.cnt = 0;
  75.         this.id = setTimeout(()=>{this.ticker()},this.delay);
  76.     };
  77.    
  78.     disarm()
  79.     {
  80.         clearTimeout(this.id);
  81.     };
  82.    
  83.     set(baseDelay=3000, ticksCount=10)
  84.     {
  85.         this.delay = baseDelay;
  86.         this.ticksCount = ticksCount;
  87.     };
  88.    
  89.     get()
  90.     {
  91.         console.log(`Ticker #${this.tickerId}\nDelay: ${this.delay}\nTicksCount: ${this.ticksCount}`);
  92.     }
  93.  
  94. }
  95.  
  96. class TickTack extends Ticker
  97. {
  98.     constructor(...args)
  99.     {
  100.         super(...args);
  101.     }
  102.     output()
  103.     {
  104.         super.output();
  105.         let now = new Date();
  106.         console.log(`Tick-Tack ${String(now.getHours()).padStart(2,0) + ':' + String(now.getMinutes()).padStart(2,0) + ':' +String(now.getSeconds()).padStart(2,0)}`);
  107.     }
  108. }
  109.  
  110. class Shape
  111. {
  112.     constructor()
  113.     {
  114.         this._sides = [];
  115.     };
  116.     _setSide(sideId, sideLength)
  117.     {
  118.         this._sides[sideId] = sideLength;
  119.     };
  120.     _getSide(sideId, sideLength)
  121.     {
  122.         return this._sides[sideId];
  123.     };
  124.     sidesInfo()
  125.     {
  126.         this._sides.forEach((it,id)=>{console.log(`Side #${id} is ${it} long`);});
  127.     };
  128.     getPerimeter()
  129.     {
  130.         return this._sides.reduce((prev,cur)=>{return prev+cur;},0)
  131.     };
  132.     getArea()
  133.     {
  134.         return 0;
  135.     };
  136.     get name()
  137.     {
  138.         return this.constructor.name;
  139.     };
  140. }
  141.  
  142. class Rectangle extends Shape
  143. {
  144.     constructor(sideA, sideB)
  145.     {
  146.         super();
  147.         this._setSide(0,sideA);
  148.         this._setSide(1,sideB);
  149.     };
  150.     getPerimeter()
  151.     {
  152.        return super.getPerimeter() * 2;
  153.     };
  154.     getArea()
  155.     {
  156.         return this._getSide(0) * this._getSide(1);
  157.     }
  158. }
  159.  
  160. class Square extends Rectangle
  161. {
  162.     constructor(sideA)
  163.     {
  164.         super(sideA,sideA);
  165.     };
  166. }
  167.  
  168. class Triangle extends Shape
  169. {
  170.     constructor(sideA, sideB, sideC)
  171.     {
  172.         super();
  173.         this._setSide(0,sideA);
  174.         this._setSide(1,sideB);
  175.         this._setSide(2,sideC);
  176.     };
  177.     getArea()
  178.     {
  179.         let p = this.getPerimeter() / 2;
  180.         return Math.sqrt(p*(p-this._getSide(0))*(p-this._getSide(1))*(p-this._getSide(2)));
  181.     }
  182. }
  183.  
  184. let shapes = [new Rectangle(4,5), new Square(6), new Triangle(3,4,5)];
  185. shapes.forEach((it,id)=>{console.log(`${id}.${it.name} Perimeter: ${it.getPerimeter()} Area: ${it.getArea()}`); it.sidesInfo();});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement