JSFiddle - React, Tailwind, and code Playground

by Sergey Shaliapin

JavaScript

// © EPAM JS lab 2016
// Prototypes, classes, OOP

// Task: remake CoffeeMachine with prototype like this:

/*
function CoffeeMachine(power) {
  this._power = power;
  this._waterAmount = 0;
}

CoffeeMachine.prototype.WATER_HEAT_CAPACITY = ... ;
CoffeeMachine.prototype._getTimeToBoil = ... ;
CoffeeMachine.prototype.run = ... ;
CoffeeMachine.prototype.setWaterAmount = ... ;
*/

function CoffeeMachine(power) {
  var waterAmount = 0;

  var WATER_HEAT_CAPACITY = 4200;

  function getTimeToBoil() {
    return waterAmount * WATER_HEAT_CAPACITY * 80 / power;
  }

  this.run = function() {
    setTimeout(function() {
      alert( 'Coffee ready!' );
    }, getTimeToBoil());
  };

  this.setWaterAmount = function(amount) {
    waterAmount = amount;
  };
}

var coffeeMachine = new CoffeeMachine(10000);
coffeeMachine.setWaterAmount(50);
coffeeMachine.run();