Pen Settings

HTML

CSS

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.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

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

              
                <head>
<meta charset="utf-8">
<link rel="icon" 
      type="image/png" 
      href="images/favicon_v2.png">
<title>Average Stock Price</title>
<link href="stock.css" rel="stylesheet" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js" defer="true" ></script>
<script src="stock.js" defer="true" ></script>
</head>
<body>
<h1>Calculate possible average price for stock holdings</h1>

<div id="inputs">
  <p class="labels">Current cost of a single share of stock:</p>
  <input id="current-cost" class="input-box" type="number" min="0" name="current-cost">
   <br>
  <p class="labels">Number of shares you hold currently:</p>
  <input id="qty-shares-held" class="input-box" type="number" min="0" name="current-holding">
   <br>
  <p class="labels">Current average cost of holdings:</p>
  <input id="crnt-avg-cost" class="input-box" type="number" min="0" name="current-avg">
   <br>
  <p class="labels">Number of shares to purchase:</p>
  <input id="qty-to-purch" class="input-box" type="number" min="0" name="qty-to-purch">
</div>
<hr>
<div id="outputs">
  <div class="out">
    <p class="olabels">Possible new average:</p>
    <p id="new-avg" class="output-box">&nbsp</p>
  </div> 
  <div class="out">
    <p class="olabels">Cost of this hypothetical transaction:</p>
    <p id="debt" class="output-box">&nbsp</p>
  </div>  
  <div class="out">
    <p class="olabels">Approximate number of shares required to reach current cost +10%:</p>
    <p id="equality-shares" class="output-box">&nbsp</p>
  </div>  
  <div class="out">
    <p class="olabels">Investment required for average to reach current cost +10%:</p>
    <p id="equality-cost" class="output-box">&nbsp</p>
  </div>
</div>
<p id="disclaimer">*Figures should not be considerd exact, but close estimations. Data are subject to<br>a certain degree of rounding error. Deviation from exact market price will decrease accuracy.</p>
              
            
!

CSS

              
                * { box-sizing: content-box;
	font-family: "Gill Sans", "Gill Sans MT", "Myriad Pro", "DejaVu Sans Condensed", Helvetica, Arial, "sans-serif";
	
}



h1 {
	font-family: Impact, Haettenschweiler, "Franklin Gothic Bold", "Arial Black", "sans-serif";
	font-size: 3em;
	color: green;
	margin-top: 75px;
	margin-left: 160px;
}

input {
	font-size: 1.1em;
	border: 1px solid lightgray;
	padding: 1px 0px 0px 6px;
	width: 90px;
}

#inputs {
	margin-top: 40px;
	margin-left: 200px;
}

#outputs {
	margin-top: 35px;
	margin-left: 200px;
	font-size: 1.1em;
	
}

.labels {
	font-size: 1.1em;
	font-weight: bold;
	display: inline;
}

.olabels {
	font-size: 1.1em;
	font-weight: bold;
	display: inline;
}

hr {
	width: 695px;
	margin-left: 200px;
}

.out {
	border-width: 2px 0px 0px 6px;
	border-color: green;
    border-style: solid;
	width: 680px;
	padding: 5px;
	margin-top: 12px;
}

.input-box {
	font-size: 1.1em;
	margin-left: 15px;
	display: inline;
	margin-bottom: 20px;
}

#new-avg {
	color: black;
	visibility: hidden;
	font-size: 2.5em;
	text-shadow: 0px 0px 10px lime;
	margin: 10px;

}

#debt {
	visibility: hidden;
	font-size: 1.3em;
	margin: 15px;
}

#equality-cost {
	visibility: hidden;
	font-size: 1.3em;
	margin: 15px;
}

#equality-shares {
	visibility: hidden;
	font-size: 1.3em;
	margin: 15px;
}

#disclaimer {
	margin-left: 200px;
	margin-bottom: 5px;
	margin-top: 100px;
	color: gray;
	font-size: .7em;
}
              
            
!

JS

              
                

	var outputBoxes = document.getElementsByClassName("output-box");
	for (e of outputBoxes){
				e.style.visibility = "hidden";
	}
	
	var outPut = document.getElementsByClassName("out");
	for (e of outPut){
				e.style.borderColor = "lightgray";
	}
	
	var outLab = document.getElementsByClassName("olabels");
	for (e of outLab){
				e.style.color = "lightgray";
	}
	
	var currentCostInput = document.getElementById("current-cost");
	currentCostInput.oninput = avgCostCalculate;
	
	var numberOfSharesInput = document.getElementById("qty-shares-held");
	numberOfSharesInput.oninput = avgCostCalculate;

	var avgCostHeldInput = document.getElementById("crnt-avg-cost");
	avgCostHeldInput.oninput = avgCostCalculate;
	
	var numSharesToPurchInput = document.getElementById("qty-to-purch");
	numSharesToPurchInput.oninput = avgCostCalculate;
	
			
	function avgCostCalculate() {
		document.getElementById("new-avg").innerHTML = "$" +
		    (((parseFloat(currentCostInput.value) * 
		    parseFloat(numSharesToPurchInput.value)) + 
		    (parseFloat(avgCostHeldInput.value) *
		    parseFloat(numberOfSharesInput.value))) / 
		    (parseFloat(numberOfSharesInput.value) +
		    parseFloat(numSharesToPurchInput.value))).toFixed(2);
		costOfTrans();
		numSharesToEqual();
		costSharesToEqual();
		showIt();
		seeIt();
		seeLab();		
	}
		
	
	function costOfTrans() {
		document.getElementById("debt").innerHTML = "$" +
			(parseFloat(numSharesToPurchInput.value) *
			parseFloat(currentCostInput.value)).toFixed(2);
			
	}
	
	function numSharesToEqual() {
		var sharesToBuy = ((parseFloat(numberOfSharesInput.value) *
			(parseFloat(avgCostHeldInput.value) - 
			(1 - 0.1) * parseFloat(currentCostInput.value))) /
			(parseFloat(currentCostInput.value) * 0.1)).toFixed(0);
		document.getElementById("equality-shares").innerHTML = sharesToBuy;
		return sharesToBuy;
			
	}
	
		
	function costSharesToEqual() {
		document.getElementById("equality-cost").innerHTML = "$" +
			(parseFloat(currentCostInput.value) * numSharesToEqual()).toFixed(2);
	}
	
	document.getElementById("new-avg").style.visibility = "visible";
	  
	function showIt() {
		console.log(numSharesToPurchInput.value);
		if (numSharesToPurchInput.value === "") {
			var outputBoxes = document.getElementsByClassName("output-box")
			for (e of outputBoxes){
				e.style.visibility = "hidden";
			}
		}
		else {
			var outputBoxes = $(".output-box");
			for (e of outputBoxes){
				e.style.visibility = "visible";
			}
		}
	}
	
	function seeIt() {
		console.log(numSharesToPurchInput.value);
		if (numSharesToPurchInput.value === "") {
			var outPut = document.getElementsByClassName("out")
			for (e of outPut){
				e.style.borderColor = "lightgray";
			}
		}
		else {
			var outPut = $(".out");
			for (e of outPut){
				e.style.borderColor = "green";
			}
		}
	}
	
	function seeLab() {
		console.log(numSharesToPurchInput.value);
		if (numSharesToPurchInput.value === "") {
			var outLab = document.getElementsByClassName("olabels")
			for (e of outLab){
				e.style.color = "lightgray";
			}
		}
		else {
			var outLab = $(".olabels");
			for (e of outLab){
				e.style.color = "black";
			}
		}
	}	
              
            
!
999px

Console