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) {
        if (this.elements[element] instanceof PowerLine != true) {
            power += this.elements[element].getPower();
            power += this.elements[element].getPowerAtNight();
        }
    }

    return power;
}

ElectricalNetwork.prototype.countPrice = function() {
	var balance = this.countPowerAt24Hours();
	var price = 0;
  
  this.elements.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 < this.elements.length; i++) {
    if (this.elements[i] instanceof PowerLine) {    
    	price += this.elements[i].countPrice(balance);
      balance = this.elements[i].countPower(balance);
  	}
	}
  
  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 >= 1 * Math.pow(10, 6) && power <= 100 * Math.pow(10, 6)) {
        this.power = power;
        this.nightpower = power;
    } else {
        throw new RangeError("power must be in range from 1 MW to 100 MW");
    }
}

Electrostation.prototype = Object.create(NetworkElement.prototype);

function SolarPanele(power) {
    if (power >=...