JSFiddle - React, Tailwind, and code Playground

JavaScript

function ElectricalNetwork(elements = []) {
    if (Array.isArray(elements)) {
        this.elements = elements;
    } else {
        throw new TypeError("elements must be an array");
    }
}

ElectricalNetwork.prototype.addElement = function(element) {
    this.elements.push(element);
}

ElectricalNetwork.prototype.countPowerAt24Hours = function() {
    var power = 0;

    for (var element in this.elements) {
      power += this.elements[element].getPower();
      power += this.elements[element].getPowerAtNight();
    }

    return power;
}

ElectricalNetwork.prototype.countEnergyTransfer = function(powerline, power) {
  var throughput = powerline.getThroughput();

  var count = (power >= 0 ) ? Math.min(throughput, power) : Math.max(-throughput, power);

  return count;
}

ElectricalNetwork.prototype.countPrice = function() {
  var balance = this.countPowerAt24Hours();
  var price = 0;
  
  var powerlines = [];
  
  for (var i = 0; i < this.elements.length; i++) {
    if (this.elements[i] instanceof PowerLine) {
      powerlines.push(this.elements[i]);
    }
  }
  
  powerlines.sort(function (a, b) {
    if (a.price > b.price) {
      return 1;
    }
    
    if (a.price < b.price) {
      return -1;
    }
    
    return 0;
  });
  
  for (var i = 0; i < powerlines.length; i++) {
    var amount = this.countEnergyTransfer(powerlines[i], balance);

    price += amount * powerlines[i].getPricePerWatt();
    balance -= amount;
  }
  
  return price;
}

function NetworkElement(power = 0, nightpower = 0) {
  this.power = power;
  this.nightpower = nightpower;
}

NetworkElement.prototype.getPower = function() {
    return this.power;
}

NetworkElement.prototype.getPowerAtNight = function() {
    return this.nightpower;
}


//power always count in a bit watts (not megawatts)
//for example, Electrostaion must be in range from 1 MW to 100 MW (1 000 000 watts to 100 000 000 watts) 
//power = 1000000
function Electrostation(power) {
    if (power < 1e6 && power > 100e6) {
...