cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <h1>Solar Panel Calculator</h1>

<p>The math might not be 100 % correct. I'm just trying to understand this shit myself.</p>

<button id="update">update view</button>

<form>
  <fieldset>
    <legend>Solar Panel</legend>
    
    <input type="number" id="solar-panel-v" value="12">
    <label for="solar-panel-v">V</label>
    <br>
    <input type="number" id="solar-panel-w" value="13">
    <label for="solar-panel-w">W</label>
    <br>
    <br>
    <label for="solar-panel-output">Output:</label><br>
    <input type="number" value="0" id="solar-panel-ah"> Ah
  </fieldset>
  <fieldset>
    <legend>Battery</legend>
    
    <input type="number" id="battery-v" value="12">
    <label for="battery-v">V</label>
    <br>
    <input type="number" step=".1" id="battery-ah" value="7">
    <label for="battery-ah">Ah</label>
    
  </fieldset>
</form>

<div id="result-solar" class="result">
  It will take %minutes% to charge the battery with the solar panel. And the battery will be able to power <a href="http://www.ikea.com/se/sv/catalog/products/40288066/">a LEDARE from IKEA</a> for %lamp_time%.
</div>

<form>
  <fieldset>
    <legend>USB Charger (12 V)</legend>
    
    <input type="number" id="charger-a" value="2.1" step=".1">
    <label for="charger-a">A</label>
    <br>
    
  </fieldset>

<fieldset>
    <legend>Phone Battery</legend>
    
    <input type="number" id="phone-mah" value="3100">
    <label for="phone-mah">mAh</label>
    <br>
    
  </fieldset>
</form>

<div id="result-phone" class="result">
  It will take %minutes% to charge your phone with the battery which was charged with the solar panel. And you'll be able to charge the phone %charge_times% times (or to %charge_percent% %) before the battery is depleted.
</div>
            
          
!
            
              var ಠ_ಠ = function(id) {
 return document.querySelector('#' + id); 
};

var resultPhoneTemplate,
    resultSolarTemplate;

function hoursToTime(decimalHours) {
  var secs = decimalHours * 3600,
      hours = Math.floor(secs / (60 * 60)),
      minutes = Math.floor((secs % (60 * 60)) / 60),
      seconds = Math.ceil((secs % (60 * 60)) % 60);
  
  return hours + 'h ' + minutes + 'm and ' + seconds + 's';
}

var batteryVolt = ಠ_ಠ('battery-v'),
    batteryAmpereHours = ಠ_ಠ('battery-ah');

var solarPanelVolt = ಠ_ಠ('solar-panel-v'),
    solarPanelWatts = ಠ_ಠ('solar-panel-w'),
    solarPanelAmpereHours = ಠ_ಠ('solar-panel-ah');

var chargerAmpere = ಠ_ಠ('charger-a'),
    phoneMilliAmpereHours = ಠ_ಠ('phone-mah');

function chargeHoursSolarBattery() {
  // Battery AH / Source A
  var bs = batteryAmpereHours.value / solarPanelAmpereHours.value;
  return bs;
}

function chargeHoursPhone() {
  // Battery AH / Source A
  var bs = (phoneMilliAmpereHours.value / 1000) / chargerAmpere.value;
  return bs;
}

function chargeTimesPhone() {
  var hours = chargeHoursPhone();
  return Math.floor(batteryAmpereHours.value / chargerAmpere.value / hours);
}

function chargePercentPhone() {
  var hours = chargeHoursPhone();
  return Math.floor((batteryAmpereHours.value / chargerAmpere.value / hours) * 100);
}

function ikeaLampTime() {
  var lampWatts = 3.5;
  var w = batteryVolt.value * batteryAmpereHours.value;
  return w / lampWatts;
}

function updateView() {
  solarPanelAmpereHours.value = solarPanelWatts.value / solarPanelVolt.value;
  
  var solarMinutes  = hoursToTime(chargeHoursSolarBattery()),
      resultSolar   = ಠ_ಠ('result-solar'),
      lampTime      = hoursToTime(ikeaLampTime()),
      text          = resultSolarTemplate;
  
  text = text.replace('%minutes%', solarMinutes);
  text = text.replace('%lamp_time%', lampTime);
  resultSolar.innerHTML = text;
  
  var phoneMinutes  = hoursToTime(chargeHoursPhone()),
      chargeTimes   = chargeTimesPhone(),
      chargePercent = chargePercentPhone(),
      resultPhone   = ಠ_ಠ('result-phone');
  
  text = resultPhoneTemplate;
  text = text.replace('%minutes%', phoneMinutes);
  text = text.replace('%charge_times%', chargeTimes);
  text = text.replace('%charge_percent%', chargePercent);
  text = text.replace('%lamp_time%', lampTime);
  resultPhone.innerHTML = text;
}

ಠ_ಠ('update').onclick = updateView;

function init() {
  resultPhoneTemplate = ಠ_ಠ('result-phone').innerHTML,
  resultSolarTemplate = ಠ_ಠ('result-solar').innerHTML;
  
  updateView();
}

init();
            
          
!
999px
Loading ..................

Console