HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URL's 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 it's URL and the proper URL extention.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<link href='https://fonts.googleapis.com/css?family=VT323|Source+Sans+Pro:400,400italic' rel='stylesheet' type='text/css'>
<div id="calcBox" class="container">
<div class="row" id="logoRow">
<div class="col-xs-3" id="logo">
<svg id="fcc-logo" class="pull-left">
<use xlink:href="#free-code-camp-logo"></use>
</svg><!--</div>
<div class="col-xs-2" id="logoText">-->
<div class="pull-left">
<h6>FREECODECAMP
<br>CALCULATOR</h6>
</div>
</div>
</div>
<div id="screenBox" class="container-fluid">
<div id="screenLogo" class="row">
<div class="col-xs-2">
<h5><strong><em>FCC</em></strong></h5>
</div>
<div class="col-xs-6 col-xs-offset-4 text-right">
<h5><em><strong>RPN</strong> SCIENTIFIC</em></h5>
</div>
</div>
<div id="LCDborder">
<div id="LCD" class="container-fluid">
<div id="indicatorMem" class="indicator">MEM</div>
<div id="indicatorOrange" class="indicator"><svg id="indicatorArrowSymbol"><use xlink:href="#unicode-arrow"></use></svg><!-- ⬏ --></div>
<div class="LCDrow row">
<div id="row2prefix" class="col-xs-1 rowPrefix">y:</div>
<div id="row2" class="col-xs-11 rowNumbers">0</div>
</div>
<div class="LCDrow row">
<div id="row1prefix" class="col-xs-1 rowPrefix">x:</div>
<div id="row1" class="col-xs-11 rowNumbers">0</div>
</div>
</div>
</div>
</div><!-- #screenBox -->
<div id="keysBox" class="container-fluid">
<div id="topRow" class="row">
<div class="col-xs-2">
<aside class="disabledShift">∑−</aside>
<button>∑+</button>
</div>
<div class="col-xs-2">
<aside>y<sup>x<!-- 𝓍 --></sup></aside>
<button class="btnOpUnary" data-op="inverse"><sup>1</sup>∕<sub>x<!-- 𝓍 --></sub></button>
</div>
<div class="col-xs-2">
<aside>x<!-- 𝓍 --><sup>2</sup></aside>
<button class="btnOpUnary" data-op="sqrt">√<span id="sqrtLine">x<!-- 𝓍 --></span></button>
</div>
<div class="col-xs-2">
<aside>10<sup>x<!-- 𝓍 --></sup></aside>
<button class="btnOpUnary" data-op="log">LOG</button>
</div>
<div class="col-xs-2">
<aside>e<sup>x<!-- 𝓍 --></sup></aside>
<button class="btnOpUnary" data-op="ln">LN</button>
</div>
<div class="col-xs-2">
<aside class="disabledShift">GTO</aside>
<button>XEQ</button>
</div>
</div>
<div class="row">
<div class="col-xs-2">
<aside class="disabledShift"><small>COMPLEX</small></aside>
<button id="btnStore">STO</button>
</div>
<div class="col-xs-2">
<aside>%</aside>
<button id="btnRecall">RCL</button>
</div>
<div class="col-xs-2">
<aside>π</aside>
<button id="btnRollDown">R ↓</button>
</div>
<div class="col-xs-2">
<aside>ASIN</aside>
<button class="btnOpUnary" data-op="sin">SIN</button>
</div>
<div class="col-xs-2">
<aside>ACOS</aside>
<button class="btnOpUnary" data-op="cos">COS</button>
</div>
<div class="col-xs-2">
<aside>ATAN</aside>
<button class="btnOpUnary" data-op="tan">TAN</button>
</div>
</div>
<div class="row">
<div class="col-xs-4">
<aside id="asideEnter" class="disabledShift">ALPHA</aside>
<button id="btnEnter">ENTER</button>
</div>
<div class="col-xs-2">
<aside class="disabledShift"><small>LAST x<!-- 𝓍 --></small></aside>
<button id="btnSwapXY">x<!-- 𝓍 --><!--≷--><small>⇆</small>y</button>
</div>
<div class="col-xs-2">
<aside class="disabledShift"><small>MODES</small></aside>
<button id="btnNegation">+/−</button>
</div>
<div class="col-xs-2">
<aside class="disabledShift"><small>DISP</small></aside>
<button class="btnNum">E</button>
</div>
<div class="col-xs-2">
<aside class="disabledShift"><small>CLEAR</small></aside>
<button id="btnBksp"><span id="bkspArrow">⬅</span></button>
</div>
</div>
<div class="row">
<div class="col-fifth">
<aside class="disabledShift">BST</aside>
<button>▲</button>
</div>
<div class="col-fifth">
<aside class="disabledShift"><small>SOLVER</small></aside>
<button class="btnNum">7</button>
</div>
<div class="col-fifth">
<aside class="disabledShift">∫ f(x<!-- 𝓍 -->)</aside>
<button class="btnNum">8</button>
</div>
<div class="col-fifth">
<aside class="disabledShift"><small>MATRIX</small></aside>
<button class="btnNum">9</button>
</div>
<div class="col-fifth">
<aside class="disabledShift"><small>STAT</small></aside>
<button class="btnOpBinary">÷</button>
</div>
</div>
<div class="row">
<div class="col-fifth">
<aside class="disabledShift">SST</aside>
<button>▼</button>
</div>
<div class="col-fifth">
<aside class="disabledShift"><small>BASE</small></aside>
<button class="btnNum">4</button>
</div>
<div class="col-fifth">
<aside class="disabledShift"><small>CONVERT</small></aside>
<button class="btnNum">5</button>
</div>
<div class="col-fifth">
<aside class="disabledShift"><small>FLAGS</small></aside>
<button class="btnNum">6</button>
</div>
<div class="col-fifth">
<aside class="disabledShift"><small>PROB</small></aside>
<button class="btnOpBinary">×</button>
</div>
</div>
<div class="row">
<div class="col-fifth">
<aside id="asideHidden">_</aside>
<button id="btnOrange"><div></div></button>
</div>
<div class="col-fifth">
<aside class="disabledShift"><small>ASSIGN</small></aside>
<button class="btnNum">1</button>
</div>
<div class="col-fifth">
<aside class="disabledShift"><small>CUSTOM</small></aside>
<button class="btnNum">2</button>
</div>
<div class="col-fifth">
<aside class="disabledShift"><small>PGM.FCM</small/></aside>
<button class="btnNum">3</button>
</div>
<div class="col-fifth">
<aside class="disabledShift"><small>PRINT</small></aside>
<button class="btnOpBinary">−</button>
</div>
</div>
<div class="row">
<div class="col-fifth">
<aside><!-- OFF --><span id="diagStyle">DIAG</span></aside>
<button id="btnExit">EXIT</button>
</div>
<div class="col-fifth">
<aside class="disabledShift"><small>TOP.FCN</small></aside>
<button class="btnNum">0</button>
</div>
<div class="col-fifth">
<aside class="disabledShift"><small>SHOW</small></aside>
<button class="btnNum">.</button>
</div>
<div class="col-fifth">
<aside class="disabledShift"><small>PRGM</small></aside>
<button>R/S</button>
</div>
<div class="col-fifth">
<aside class="disabledShift"><small>CATALOG</small></aside>
<button class="btnOpBinary">+</button>
</div>
</div>
</div><!-- #keysBox -->
</div><!-- #caclBox -->
<footer>
<p class="text-center"><small>Project created from scratch by <a href="http://andreweast.net" target="_none">Andrew Reid East</a></small></p>
<p class="text-center"><small><a href="https://en.wikipedia.org/wiki/HP-42S" target="_none">RPN Scientific 42S</a> calculator designed by HP (<a href="http://thomasokken.com/free42/42s.pdf" target="_none">manual</a>)</small></p>
</footer>
<div id="diagBox">
<header>DIAGNOSTIC DATA</header>
<p><label>T: <input id="stack3" type="text"></label></p>
<p><label>Z: <input id="stack2" type="text"></label></p>
<p><label>Y: <input id="stack1" type="text"></label></p>
<p><label>X: <input id="stack0" type="text"></label></p>
<hr>
<p><label>> <input id="entryReg" type="text"></label></p>
<div id="showDiagMem">
<hr>
<p><label>m: <input type="text" id="diagMem"></label></p>
</div>
</div>
<!-- Here follows the HIDDEN svg definitions -->
<svg style="display:none;" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg">
<symbol id="free-code-camp-logo" viewBox="0 0 400 300">
<g>
<path d="m166.8,41.35c0,0 13.4,9.3 13.4,26c0,28.8 -39.1,60.9 -39.1,90.8c0,32.4 24.7,53.5 48.7,59.9c3.3,0.8 4.8,-3.2 0.7,-4.2c-9.7,-2.4 -20.5,-15.1 -20.5,-35c0,-18.3 17.7,-25.4 24.9,-34.4c7.3,-9.1 5.6,-20.8 1.3,-24.1c-4.2,-3.4 -1.4,-7.9 6.3,-2.5c7.8,5.4 14,15.4 11.7,25c-3,12.2 0.9,19.2 7.3,20.1s13.4,-2.3 12.7,-8.2c-0.7,-5.9 -4.2,-12.1 -2.1,-12.1c4.5,0 14.1,11.2 14.1,28.8c0,17.7 -14.6,30.1 -19.1,34.7c-2.7,2.8 0.9,6.4 4.5,4.8c1.9,-0.8 5.8,-3.1 8.2,-4.8c11.6,-8.3 29.6,-24.9 29.6,-55.8c0,-32.3 -12.3,-46.8 -24.6,-58.9c-12.3,-12.1 -15.5,-9 -12,-3.1c10.6,18.1 4.2,24.2 -3.6,24.2c-9,0 -9.5,-20.5 -13,-36.1c-7.8,-34.7 -39.1,-40.6 -45.1,-40.9c-6.1,-0.7 -11.3,2.7 -4.3,5.8z"
id="svg_3" />
<path d="m13.7,144.95c0,-39.7 13.8,-75.1 41.4,-106.3c10.1,-11.4 18,-17.2 23.5,-17.2c2.1,0 3.9,0.8 5.6,2.3c1.7,1.5 2.5,3.2 2.5,5.2c0,2.9 -3.5,7.7 -10.6,14.5c-28.8,27.7 -43.2,61.9 -43.2,102.6c0,45.2 14.9,82 44.8,110.5c6,5.7 9,10.3 9,13.7c0,2.2 -0.8,4.1 -2.5,5.7s-3.5,2.5 -5.7,2.5c-6.9,0 -16.7,-8.1 -29.4,-24.2c-23.6,-30.1 -35.4,-66.5 -35.4,-109.3z"
id="svg_4" />
<path d="m386.3,154.95c0,39.8 -13.8,75.2 -41.4,106.4c-10.1,11.4 -18,17.2 -23.5,17.2c-2.1,0 -3.9,-0.8 -5.6,-2.3c-1.7,-1.5 -2.5,-3.3 -2.5,-5.3c0,-2.8 3.5,-7.6 10.6,-14.4c28.8,-27.6 43.2,-61.8 43.2,-102.7c0,-45.1 -14.9,-82 -44.8,-110.5c-6,-5.7 -9,-10.2 -9,-13.5c0,-2.2 0.8,-4.1 2.5,-5.8c1.6,-1.7 3.5,-2.5 5.7,-2.5c6.8,0 16.5,8.1 29.3,24.3c23.6,30.2 35.5,66.6 35.5,109.1z"
id="svg_5" />
<path d="m294,259.05l-174.7,0c-5.7,0 -10.4,-4.6 -10.4,-10.4s4.6,-10.4 10.4,-10.4l174.7,0c5.7,0 10.4,4.6 10.4,10.4s-4.7,10.4 -10.4,10.4z" id="svg_6" />
</g>
</symbol>
</svg>
<svg style="display:none;" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg">
<!-- Created with SVG-edit - https://svg-edit.googlecode.com/ -->
<symbol id="unicode-arrow" viewBox="0 0 260 120">
<g>
<title>Layer 1</title>
<rect x="0" y="100" width="220" height="20"/>
<rect x="200" y="0" width="20" height="120"/>
<rect x="180" y="20" width="20" height="20"/>
<rect x="160" y="40" width="20" height="20"/>
<rect x="240" y="40" width="20" height="20"/>
<rect x="220" y="20" width="20" height="20"/>
</g>
</symbol>
</svg>
/* Background pattern from subtlepatterns.com */
$DEBUG: false;
$calcBG: #3C3C36;
$silverLogo: #909289;
$logoBG: #32302B;
$LCDborder: #51514C;
$LCDborderLogo: white;
$LCDsubBorder: #58595A;
$LCDbg: #6B836B;
$LCDtext: #0B2327;
$buttonBezel: #433C38;
$buttonBezelTopRow: #51514C;
$buttonBG: #212121;
$buttonText: #D5D3CF;
$buttonOrange: #FC8C4A;
body {
font-family: "Source Sans Pro", sans-serif;
}
svg {
fill: currentColor; /* Lets SVG take on parent element's text color */
vertical-align: bottom;
}
small {
font-size: 80%;
}
button.disabled {
border: 2px inset $buttonBG;
color: #444;
}
#diagStyle {
font-family: Consolas, monospace;
}
#diagBox {
display: none;
@if $DEBUG {
display: initial;
}
header {
background-color: $LCDborder;
text-align: center;
color: $LCDborderLogo;
padding: 5px 0;
}
p {
margin: 5px;
padding-left: 3px;
background-color: $LCDbg;
}
input {
background-color: $LCDbg;
border: none;
font-family: VT323, Consolas, monospace;
font-size: 120%;
color: $LCDtext;
}
position: absolute;
top: 10px;
left: 10px;
border: 2px solid $calcBG;
border-radius: 8px;
background-color: $silverLogo;
font-size: 120%;
color: $LCDtext;
}
#calcBox {
background-color: $calcBG;
background-image: url(http://dev.andreweast.net/FreeCodeCamp/pattern_colored_ticks_@2X.png);
//background-image: url(pattern_colored_ticks_@2X.png);
width: 318px;
border: 2px outset $calcBG;
border-radius: 12px;
margin-top: 10px;
margin-bottom: 10px;
}
#logoRow {
color: $silverLogo;
padding-top: 20px;
padding-bottom: 10px;
}
#logo {
background-color: $logoBG;
border: 2px groove $calcBG;
border-radius: 2px;
height: 37px;
width: 135px;
padding-left: 3px;
padding-right: 3px;
margin-left: 25px;
padding-top: 3px;
padding-bottom: 3px;
}
#logo svg{
height: 26px;
width: 35px;
}
#logo h6 {
margin: 0 0 0 3px;
}
#screenBox {
border-top-left-radius: 8px;
border-top-right-radius: 8px;
background-color: $LCDborder;
color: $LCDborderLogo;
}
#screenLogo h5 {
padding-left: 10px;
padding-right: 10px;
margin-top: 15px;
margin-bottom: 5px;
}
#LCDborder {
background-color: $LCDsubBorder;
padding: 10px 3px;
border: 1px inset $LCDsubBorder;
}
#LCD {
font-family: VT323, Consolas, monospace;
font-variant: small-caps;
background-color: $LCDbg;
color: $LCDtext;
padding-top: 15px;
padding-left: 3px;
padding-right: 3px;
}
.indicator {
display: none;
color: $LCDbg;
background-color: $LCDtext;
border-radius: 2px;
font-size: 0.9em;
line-height: 0.9;
padding: 2px 4px;
margin-top: -11px;
position: absolute;
}
#indicatorMem {
margin-left: 176px
}
#indicatorOrange {
margin-left: 39px;
color: $buttonOrange;
padding: 1px 5px;
}
#indicatorArrowSymbol {
width: 12px;
height: 13px;
}
.LCDrow {
line-height: 1.2;
font-size: 1.8em;
//background-color: rgba(50,50,50,0.5);
margin-left: -3px;
margin-right: -3px;
}
.LCDrow > div {
padding-left: 3px;
padding-right: 3px;
}
#row1, #row2 {
padding-left: 8px;
}
#keysBox {
border-bottom-left-radius: 12px;
border-bottom-right-radius: 12px;
background-color: $buttonBezel;
color: $buttonText;
border-right: 2px inset $buttonBG;
border-bottom: 2px inset $buttonBG;
border-left: 2px inset #999;
margin-bottom: 15px;
padding-top: 5px;
padding-bottom: 10px;
padding-left: 5px;
padding-right: 5px;
}
#keysBox > div {
height: 46px;
margin-right: -5px;
margin-left: -5px;
}
#keysBox > div > div {
padding-left: 5px;
padding-right: 5px;
}
#keysBox button {
background-color: $buttonBG;
border: 2px outset $buttonBG;
border-radius: 3px;
font-size: 1em;
padding: 0;
width: 30px;
margin-left: 3px;
margin-right: 3px;
height: 100%;
}
#keysBox button:active {
border: 2px inset $buttonBG;
}
#keysBox button:focus {
outline: gray auto 5px;
//outline: none;
}
#keysBox aside {
font-size: 0.8em;
color: $buttonOrange;
text-align: center;
width: 34px;
}
#keysBox aside.disabledShift {
color: #964412; //#933903;
// filter: saturate(50%);
}
.col-fifth {
width: 20%; // 1/5th, similair to Bootstrap's 1/12th grid columns
float: left;
position: relative;
min-height: 1px;
}
#keysBox .col-fifth button {
width: 39.25px;
}
#keysBox .col-fifth aside {
width: 43.25px;
}
button#btnEnter { //note: "eleent#id" is necessary here to override specificty of "#parent element"
width: 76px;
}
aside#asideEnter {
width: 82px; //btnEnter's width of 76px + 3*2 margins
}
aside#asideHidden {
color: $buttonBezel;
}
#btnOrange > div {
background-color: $buttonOrange;
width: 90%;
height: 16px;
margin: 2px auto;
}
// #topRow {
// background-color: $buttonBezelTopRow;
// }
#sqrtLine {
border-top: 1px solid $buttonText;
}
// #bkspArrow {
// font-size: 150%;
// line-height: 20px;
// }
.btnNum {
font-weight: bold;
}
var DEBUG = true;
$(document).ready(function() {
$("button").addClass("disabled");
var stack = new RPNStack();
var trigModeDegrees = true;
var shiftMode = false;
var shiftToggle = function() {
shiftMode = (shiftMode) ? false : true;
$("#indicatorOrange").toggle(shiftMode);
};
var shiftOff = function() {
shiftMode = false;
$("#indicatorOrange").toggle(shiftMode);
}
$("#btnOrange").on("click", function() {
shiftToggle();
}).removeClass("disabled");
$("#btnExit").on("click", function() {
if (shiftMode)
$("#diagBox").toggle();
else
; // exit functionality?
shiftOff();
}).removeClass("disabled");
$(".btnNum").on("click", function() {
stack.addToEntryRegister(this.innerHTML);
shiftOff();
}).removeClass("disabled");
$("#btnEnter").on("click", function() {
if (!shiftMode) {
stack.commitEntryRegister();
stack.keyEnter();
} else {
//key "ALPHA" not implemented
}
shiftOff();
}).removeClass("disabled");
$(".btnOpBinary").on("click", function() {
stack.commitEntryRegister();
var result = 0;
switch (this.innerHTML) {
case "+":
// var op1 = stack.pop();
// var op2 = stack.pop();
// result = op1 + op2;
// console.log(op1 + "+" + op2 + "=" + result);
result = stack.pop() + stack.pop();
break;
case "−":
var op2 = stack.pop();
result = stack.pop() - op2;
break;
case "×":
result = stack.pop() * stack.pop();
break;
case "÷":
var op2 = stack.pop();
result = stack.pop() / op2;
break;
default:
console.log("btnOpBinary passed bad operator: " + this.innerHTML);
}
stack.push(result);
shiftOff();
}).removeClass("disabled");
$(".btnOpUnary").on("click", function() {
stack.commitEntryRegister();
switch (this.dataset.op) {
case "sqrt":
if (shiftMode) {
var base = stack.pop();
result = base * base;
} else {
result = Math.sqrt(stack.pop());
}
break;
case "inverse":
if (shiftMode) {
var exponent = stack.pop();
result = Math.pow(stack.pop(), exponent);
} else {
result = 1 / stack.pop();
}
break;
case "ln":
if (shiftMode) {
result = Math.pow(Math.E, stack.pop());
} else {
//TODO: check for NaN results of the log functions, ie. log(negative)
result = Math.log(stack.pop()); //in JS, Math.log(x) means ln(x)
}
break;
case "log":
if (shiftMode) {
result = Math.pow(10, stack.pop());
} else {
result = Math.log10(stack.pop());
}
break;
case "sin":
if (!shiftMode) {
result = Math.sin((trigModeDegrees) ? (stack.pop() * (Math.PI / 180)) : stack.pop());
} else {
result = Math.asin((trigModeDegrees) ? (stack.pop() * (Math.PI / 180)) : stack.pop());
}
break;
case "cos":
if (!shiftMode) {
result = Math.cos((trigModeDegrees) ? (stack.pop() * (Math.PI / 180)) : stack.pop());
} else {
result = Math.acos((trigModeDegrees) ? (stack.pop() * (Math.PI / 180)) : stack.pop());
}
break;
case "tan":
if (!shiftMode) {
result = Math.tan((trigModeDegrees) ? (stack.pop() * (Math.PI / 180)) : stack.pop());
} else {
result = Math.atan((trigModeDegrees) ? (stack.pop() * (Math.PI / 180)) : stack.pop());
}
break;
default:
}
stack.push(result);
shiftOff();
}).removeClass("disabled");
$("#btnRollDown").on("click", function() {
if (!shiftMode) {
//TODO: this also needs to (conditionally?) commit eRegister!
stack.rollStackDown();
} else {
stack.commitEntryRegister();
stack.push(Math.PI);
}
shiftOff();
}).removeClass("disabled");
$("#btnNegation").on("click", function() {
stack.negation();
shiftOff();
}).removeClass("disabled");
$("#btnSwapXY").on("click", function() {
stack.swap();
shiftOff();
}).removeClass("disabled");
$("#btnBksp").on("click", function() {
stack.bksp();
shiftOff();
}).removeClass("disabled");
$("#btnStore").on("click", function() {
if (!shiftMode) {
stack.sto();
} else {
//button "COMPLEX" not implemeted
}
shiftOff();
}).removeClass("disabled");
$("#btnRecall").on("click", function() {
if (!shiftMode) {
stack.rcl();
} else {
stack.commitEntryRegister();
var percent = stack.pop();
var total = stack.pop();
var part = percent * total / 100;
stack.push(total); //HP 42s leaves the total on the stack
stack.push(part);
}
shiftOff();
}).removeClass("disabled");
//this function must be after all other event binding to allow jQuery to call them in that order!
// $("button").on("click", function() {
// shiftOff();
// });
});
//An object that simulate's the HP-42S internal memory.
//Has three registers:
// 1. An "Entry Register", which stores a string (to allow LTR entry), and is validated after entry is committed
// 2. The 4-item stack. X & Y, which are show on the LCD, and Z & T above.
// The stack copies the top item "T" down if the stack is popped()
// 3. A single memory location
//Use the "DIAG" function on the "Exit" key to view all the memory locations.
//To separate functionality and maintain privacy within this object, all GUI functions are outside this class, as if the hardware was sending button commands to a processor & memory IC
var RPNStack = function() {
//using C#-style underscore to designate Private variables
var _stack = [0,0,0,0]; // X, Y, Z, T
var _entryRegister = "";
var _hasDecimal = false;
var _hasExponent = false;
var _wasLastEntryHardCommit = false; //controls if new input onto the entryRegister should NOT push the stack up. ie. after [ENTER]
var _mem = 0;
function debugLog(func) {
console.log(func + ":" + _stack[0] + "," + _stack[1] + "," + _stack[2] + "," + _stack[3] + "," + _entryRegister);
}
//Pushes the registers "onto the GUI busses", to use the hardware term
var refresh = function() {
//LCD locations
if (_entryRegister === "") {
$("#row1").text(_stack[0]);
} else {
$("#row1").text(_entryRegister + "_");
}
$("#row2").text(_stack[1]);
//Memory idicator on the LCD
if (_mem === 0) {
$("#indicatorMem, #showDiagMem").hide();
} else {
$("#indicatorMem, #showDiagMem").show();
}
//DIAG text boxes
$("#entryReg").val(_entryRegister);
$("#diagMem").val(_mem);
for (var i = 0; i < _stack.length; ++i)
$("#stack" + i).val(_stack[i]);
if (DEBUG) debugLog("refresh");
}
this.pop = function() {
var result = _stack[0];
//return x. roll down & copy T down onto Z
_stack[0] = _stack[1];
_stack[1] = _stack[2];
_stack[2] = _stack[3];
if (DEBUG) debugLog("pop");
if (DEBUG) refresh();
return result;
};
this.push = function(num) {
//add a new value at the bottom, push the rest upward
_stack[3] = _stack[2]; //lose value in T
_stack[2] = _stack[1];
_stack[1] = _stack[0];
_stack[0] = num;
if (DEBUG) debugLog("push");
refresh();
};
//Add a single digit to the Entry Register(or decimal or "E" for scientific notation)
this.addToEntryRegister = function(digit) {
if (DEBUG) console.log("addToEntryReg("+digit+")");
//Preserve the value in x
// unless the last keystroke was "ENTER", then don't push the stack up.
if (_entryRegister === "") {
if (!_wasLastEntryHardCommit)
this.push(0);
else
_wasLastEntryHardCommit = false;
}
if (digit[0] >= "0" && digit[0] <= "9") {
_entryRegister += digit[0];
} else if (digit[0] === "." && !_hasDecimal) {
if (_entryRegister === "")
_entryRegister = "0.";
else
_entryRegister += ".";
_hasDecimal = true;
} else if (digit[0] === "E" && !_hasExponent) {
if (_entryRegister === "")
_entryRegister = "1e";
else
_entryRegister += "e";
_hasExponent = true;
}
if (DEBUG) debugLog("addEReg");
refresh();
}
this.commitEntryRegister = function() {
if (_entryRegister !== "") {
_stack[0] = parseFloat(_entryRegister);
_entryRegister = "";
_hasDecimal = false;
_hasExponent = false;
}
if (DEBUG) debugLog("commitEReg");
}
this.rollStackDown = function() {
//rotate the whole stack, erase no values;
var temp = _stack[0];
_stack[0] = _stack[1];
_stack[1] = _stack[2];
_stack[2] = _stack[3];
_stack[3] = temp;
if (DEBUG) debugLog("rollDown");
refresh();
};
this.keyEnter = function() {
this.push(_stack[0]);
_wasLastEntryHardCommit = true;
if (DEBUG) debugLog("enter");
};
this.negation = function() {
if (_entryRegister !== "") {
if (_entryRegister[0] === "-")
_entryRegister = _entryRegister.slice(1);
else
_entryRegister = "-" + _entryRegister;
} else {
_stack[0] = _stack[0] * -1;
}
if (DEBUG) debugLog("negation");
refresh();
};
this.swap = function() {
this.commitEntryRegister();
var temp = _stack[0];
_stack[0] = _stack[1];
_stack[1] = temp;
if (DEBUG) debugLog("swapX&Y");
refresh();
};
this.bksp = function () {
if (_entryRegister !== "") {
//TODO: check how clearing the entryRegister interacts with _wasLastEntryHardCommit ie. should it flip _wasLastEntryHardCommit? (try on real calc)
if (_entryRegister.length === 1) {
_entryRegister = "";
} else {
_entryRegister = _entryRegister.slice(0, _entryRegister.length - 1);
if (_entryRegister === "-")
_entryRegister = "";
}
} else {
_stack[0] = 0;
}
if (DEBUG) debugLog("backspace");
refresh();
};
this.sto = function() {
this.commitEntryRegister();
_mem = _stack[0];
if (DEBUG) debugLog("sto mem=" + _mem);
refresh(); //also shows/hides indicatorMem
}
this.rcl = function() {
this.commitEntryRegister();
this.push(_mem);
if (DEBUG) debugLog("rcl mem=" + _mem);
refresh();
}
//Part of constructor: call refresh() after all private variables are set.
refresh();
};
Also see: Tab Triggers