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.countRemainingPowerAfterPass = function(powerline, power) {
    var throughput = powerline.getThroughput();

    for (var i = 0; i < throughput; throughput--) {
      if (power == 0) {
        break;
      }
      
      power += power / -Math.abs(power);
    }
  
  return power;
}

ElectricalNetwork.prototype.countPowerLinePrice = function(powerline, power) {
    var price = 0;
    var throughput = powerline.getThroughput();

    for (var i = 0; i < throughput; throughput--) {
      if (power == 0) {
        break;
      }
    	
      //price counting for each watt with sign oppside power
      price += powerline.getPrice() / Math.pow(10, 6) * (power / -Math.abs(power));
      power += power / -Math.abs(power);
    }
  
  return price;
}

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++) {
    price += this.countPowerLinePrice(powerlines[i], balance);
    balance = this.countRemainingPowerAfterPass(powerlines[i], balance);
	}
  
  return...