Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Ebike Estimated Battery Lifetime</h1>
<p>This estimation is based on the assumption of using the bike on weekdays to and from work.</p>
<div id="hint">Note that actual battery life is affected by many factors, mainly how you charge it: Try to not charge and discharge it fully - </br> charge it to ~80-90% and discharge it to ~20% before charging again. If you want to store the battery / not use it for a while, charge it to ~50%. </br> The battery lifetime does not mean that it will be completely unusable after X years, just that it will have lost a considerable amount of capacity (~20%),</br> meaning the range in one charge is decreased, and potentially lower voltage (which means slightly lower top speed).</div>
<input type="checkbox" id="useimperical" onclick="onunitsclicked()">
<label for="useimperical">Use imperial units</label><br>
<div class="slidercontainer">
  <span>Distance to work:</span>
  <input type="range" min="1" max="50" value="10" class="slider" id="workdistance">
  <span id="workdistancevalue" class="value"></span>
</div>
<br/>
<div class="slidercontainer">
  <span>Range per charge:</span>
  <input type="range" min="10" max="200" value="50" class="slider" id="range">
  <span id="rangevalue" class="value"></span>
</div>
<br/>
<div class="slidercontainer">
  <span>Number of charge cycles:</span>
  <input type="range" min="100" max="1000" value="600" class="slider" id="cycles">
  <span id="cyclesvalue" class="value"></span>
</div>
<br/>
<p>Estimated lifetime: <span id="lifetime"></span> years</p>
<br/>
<h2>Cost estimation</h2>
<div class="slidercontainer">
  <span>Battery cost:</span>
  <input type="range" min="100" max="1000" value="500" class="slider" id="cost">
  <span id="costvalue" class="value"></span>
</div>
<p>Estimated cost: <span id="costperyear"></span>/year</p>
              
            
!

CSS

              
                * {
  font-family: Arial;
}

body {
  padding: 20px;
}

.slidercontainer {
  display: flex;
  height: 60px;
  flex-direction: row;
  align-items: center;
}

#useimperical {
  margin-bottom: 20px;
}

#hint {
  opacity: 0.7;
  margin-bottom: 30px;
}

span {
  font-size: 14pt;
}

p {
  font-size: 16pt;
  font-weight: bold;
  margin-top: 20px;
  margin-bottom: 30px;
}

.slider {
  width: 800px;
  margin: 20px;
}

.value {
  font-weight: bold;
}

#lifetime, #costperyear {
  font-size: 26pt;
}
              
            
!

JS

              
                var useimperical = false;

function calcLifetime(range, cycles, workdistance) {
  var lifetime = cycles*range/(workdistance*2)/(365*(5/7));
  return Math.round(lifetime * 10) / 10
}

function updateLifetime() {
  var range = parseInt(rangeslider.value);
  var cycles = parseInt(cyclesslider.value);
  var workdistance = parseInt(workdistanceslider.value);
  var lt = calcLifetime(range, cycles, workdistance);
  document.getElementById("lifetime").innerHTML = lt;
  return lt;
}

var rangeslider = document.getElementById("range");
rangeslider.step = "5"
var rangevalue = document.getElementById("rangevalue");

var cyclesslider = document.getElementById("cycles");
cyclesslider.step = "25"; 
var cyclesvalue = document.getElementById("cyclesvalue");

var workdistanceslider = document.getElementById("workdistance");
var workdistancevalue = document.getElementById("workdistancevalue");

var costslider = document.getElementById("cost");
costslider.step = "25";
var costvalue = document.getElementById("costvalue");

var costperyear = document.getElementById("costperyear");

function rendervalues(useimpericalunits) {
  var distanceunit = useimperical ? " miles" : " km";
  var monetaryunit = useimperical ? " USD" : " EUR";
  
  rangevalue.innerHTML = rangeslider.value + distanceunit; 
  cyclesvalue.innerHTML = cyclesslider.value + " cycles";
  workdistancevalue.innerHTML = workdistanceslider.value + distanceunit;
  costvalue.innerHTML = costslider.value + monetaryunit;
  costperyear.innerHTML = Math.round(parseInt(costslider.value) / updateLifetime()) + monetaryunit;
}
rendervalues(useimperical);

rangeslider.oninput = function() {
  rangevalue.innerHTML = this.value + " km";
  updateLifetime();
  updatecostperyear()
} 

cyclesslider.oninput = function() {
  cyclesvalue.innerHTML = this.value + " cycles";
  updateLifetime();
  updatecostperyear()
}

workdistanceslider.oninput = function() {
  workdistancevalue.innerHTML = this.value + " km";
  updateLifetime();
  updatecostperyear()
}

function onunitsclicked() {
  var useimpericalcheck = document.getElementById("useimperical");
  useimperical = useimpericalcheck.checked;  
  rendervalues(useimperical);
}

function updatecostperyear() {
   var monetaryunit = useimperical ? " USD" : " EUR";
   costperyear.innerHTML = Math.round(parseInt(costslider.value) / updateLifetime()) + monetaryunit;
}

costslider.oninput = function() {
  var monetaryunit = useimperical ? " USD" : " EUR";
  
  costvalue.innerHTML = this.value + monetaryunit;
  updatecostperyear()
}

updateLifetime();
              
            
!
999px

Console