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.
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
</head>
<div class="container-fluid" style="zoom:100%;" #id="calculator">
<div class="calc-screen-area">
<textarea class="calc-screen" id="calc-screen"></textarea>
</div>
<div class="calc-pad">
<a href="#" class="btn calc-btn-small cal-btn-orange disabled" style="font-size:8px; line-height:22px;">SHIFT</a>
<a href="#" class="btn calc-btn-small disabled" style="font-size:9px; line-height:22px;">OPTN</a>
<a href="#" class="btn calc-btn-small disabled" style="font-size:9px; line-height:22px;">VARS</a>
<a href="#" class="btn calc-btn-small disabled" style="font-size:9px; line-height:22px;">MENU</a>
<a href="#" class="btn calc-btn-small calc-btn-small-textsmall">Ans</a>
<a href="#" class="btn calc-btn-small calc-btn-small-textsmall cal-btn-red" ccmd="FIT">FIT</a>
<BR>
<a href="#" class="btn calc-btn-small cal-btn-red disabled " style="font-size:8px; line-height:22px;">ALPHA</a>
<a href="#" class="btn calc-btn-small" ccmd="SQUARED">𝑥<sup style="font-size:9px;">2</sup></a>
<a href="#" class="btn calc-btn-small" style="font-size:20px; line-height:25px;" ccmd="POWER">^</a>
<a href="#" class="btn calc-btn-small" style="font-size:10px; line-height:21px;">EXIT</a>
<a href="#" class="btn calc-btn-small calc-btn-small-textsmall"> </a>
<a href="#" class="btn calc-btn-small calc-btn-small-textsmall" id="info-icon" data-toggle="modal" data-target="#infoModal">INFO </a>
<br>
<a href="#" class="btn calc-btn-small disabled" style="font-size:10px; line-height:21px;">X,T</a>
<a href="#" class="btn calc-btn-small calc-btn-small-textsmall">log</a>
<a href="#" class="btn calc-btn-small calc-btn-small-textsmall">ln</a>
<a href="#" class="btn calc-btn-small calc-btn-small-textsmall">sin</a>
<a href="#" class="btn calc-btn-small calc-btn-small-textsmall">cos</a>
<a href="#" class="btn calc-btn-small calc-btn-small-textsmall">tan</a>
<br>
<a href="#" class="btn calc-btn-small disabled">a<sup>c</sup>/<sub>u</sub></a>
<a href="#" class="btn calc-btn-small disabled" style="font-size:10px; line-height:21px;">F/D</a>
<a href="#" class="btn calc-btn-small">(</a>
<a href="#" class="btn calc-btn-small">)</a>
<a href="#" class="btn calc-btn-small disabled" style="line-height:13px">,</a>
<a href="#" class="btn calc-btn-small disabled">➡</a>
<br>
<div style="margin:2px; font-size:1px; line-height:2px;">  </div>
<div class="calc-btn-top-markings">
<span class="calc-btn-alt-left"> </span><span class="calc-btn-alt-right">M</span>
<span class="calc-btn-alt-left"> </span><span class="calc-btn-alt-right">N</span>
<span class="calc-btn-alt-left"> </span><span class="calc-btn-alt-right">O</span>
<span class="calc-btn-alt-center">INS</span>
<span class="calc-btn-alt-center">OFF</span>
</div>
<a href="#" class="btn calc-btn">7</a>
<a href="#" class="btn calc-btn">8</a>
<a href="#" class="btn calc-btn">9</a>
<a href="#" class="btn calc-btn cal-btn-grey calc-btn-textsmall" ccmd="DEL" >DEL</a>
<a href="#" class="btn calc-btn cal-btn-grey" style="font-size: 16px" ccmd="AC">AC<sup style="font-size: 9px; vertical-align:middle;">/ON</sup></a>
<div class="calc-btn-top-markings">
<span class="calc-btn-alt-left"> </span><span class="calc-btn-alt-right">P</span>
<span class="calc-btn-alt-left"> </span><span class="calc-btn-alt-right">Q</span>
<span class="calc-btn-alt-left"> </span><span class="calc-btn-alt-right">R</span>
<span class="calc-btn-alt-left"> </span><span class="calc-btn-alt-right">S</span>
<span class="calc-btn-alt-left"> </span><span class="calc-btn-alt-right">T</span>
</div>
<a href="#" class="btn calc-btn">4</a>
<a href="#" class="btn calc-btn">5</a>
<a href="#" class="btn calc-btn">6</a>
<a href="#" class="btn calc-btn" style="line-height:27px" ccmd="MULTIPLY">×</a>
<a href="#" class="btn calc-btn" style="line-height:27px" ccmd="DIVIDE">÷</a>
<div class="calc-btn-top-markings">
<span class="calc-btn-alt-left"> </span><span class="calc-btn-alt-right">U</span>
<span class="calc-btn-alt-left"> </span><span class="calc-btn-alt-right">V</span>
<span class="calc-btn-alt-left"> </span><span class="calc-btn-alt-right">W</span>
<span class="calc-btn-alt-left"> </span><span class="calc-btn-alt-right">X</span>
<span class="calc-btn-alt-left"> </span><span class="calc-btn-alt-right">Y</span>
</div>
<a href="#" class="btn calc-btn">1</a>
<a href="#" class="btn calc-btn">2</a>
<a href="#" class="btn calc-btn">3</a>
<a href="#" class="btn calc-btn" style="line-height:27px" ccmd="ADD">+</a>
<a href="#" class="btn calc-btn" style="line-height:27px" ccmd="MINUS">−</a>
<div class="calc-btn-top-markings">
<span class="calc-btn-alt-left"> </span><span class="calc-btn-alt-right">Z</span>
<span class="calc-btn-alt-left"> </span><span class="calc-btn-alt-right"> </span>
<span class="calc-btn-alt-left"> </span><span class="calc-btn-alt-right"> </span>
<span class="calc-btn-alt-center">Ans</span>
<span class="calc-btn-alt-left"> </span><span class="calc-btn-alt-right"> </span>
</div>
<a href="#" class="btn calc-btn" lcmd="" ccmd="" rcmd="">0</a>
<a href="#" class="btn calc-btn" lcmd="" ccmd="DECIMAL POINT" rcmd="" style="line-height:27px">·</a>
<a class="btn calc-btn calc-btn-textsmall" ccmd="EXPONENT">EXP</a>
<a class="btn calc-btn calc-btn-textsmall" style="line-height:27px" ccmd="UNERY NEGATIVE">(−)</a>
<a class="btn calc-btn calc-btn-textsmall cal-btn-purple" ccmd="EXECUTE">EXE</a>
</div>
</div>
<!-- Info Modal -->
<div class="modal fade" id="infoModal" tabindex="-1" role="dialog" aria-labelledby="myinfoLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" id="closeModalCross" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<span class="modal-title" id="myModalLabel"><i class="fa fa-info-circle fa-lg"></i> Calculator</span>
</div>
<div class="modal-body">
<a href="https://codepen.io/sscchan/full/EPNRxq/" target="_blank">
View Portfolio</a>
<br><br>
<b>Info:</b><br>
This pen is created as part of FreeCodeCamp's "Build a Javascript Calculator" exercise. Since there are no Android emulator for the Casio fx-9860, this pen aims to recreate part of the basic calculation function of that calculator so it can be used on an Android phone (or other devices). It is designed to fulfill the following user stories:
<br>
<ol>
<li>I can add, subtract, multiply and divide two numbers.</li>
<li>I can clear the input field with a clear button.</li>
<li>I can keep chaining mathematical operations together until I hit the equal button, and the calculator will tell me the correct output.</li>
</ol>
</div>
</div>
</div>
</div>
</body>
body {
background-color: #fff;
}
.calculator {
margin-left: auto;
margin-right: auto;
}
.calc-screen-area {
border-radius: 10px 10px 7px 7px;
background-color: rgb(49, 59, 95);
padding: 15px 10px 10px 10px;
width: 288px;
margin-left: auto;
margin-right: auto;
}
.calc-screen {
width: 268px;
height: 152px;
border: solid;
border-width: 14px;
border-color: rgb(221, 231, 240);
border-radius: 6px;
background-color: #fff;
font-family: 'VT323';
font-size: 22px;
line-height: 20px;
}
.calc-pad {
background-color: rgb(211, 215, 218);
text-align: center;
vertical-align: middle;
width: 280px;
padding: 5px 0px 15px 0px;
border-radius: 0px 0px 14px 14px;
margin: 0px 4px 0px 4px;
margin-left: auto;
margin-right: auto;
}
.calc-btn-top-markings {
height: 13px;
line-height: 16px;
padding: 0px;
margin: 0px 2px 0px 2px;
font-size: 10px;
}
.calc-btn-alt-left {
display: inline-block;
width: 23px;
margin: 0px 0px 0px 2px;
padding: 0px 1px 0px 4px;
text-align: left;
color: rgb(158, 152, 55);
font-weight: bold;
}
.calc-btn-alt-right {
display: inline-block;
width: 23px;
margin: 0px 2px 0px 0px;
padding: 0px 0px 0px 1px;
text-align: right;
color: rgb(175, 90, 119);
font-weight: bold;
}
.calc-btn-alt-center {
display: inline-block;
width: 45px;
margin: 0px 2px 0px 2px;
padding: 0px;
text-align: center;
color: rgb(158, 152, 55);
font-weight: bold;
}
.calc-btn {
color: #fff;
font-family: "verdana";
text-align: center;
background-color: rgb(66, 72, 104);
width: 45px;
height: 31px;
border-radius: 8px 8px 11px 11px;
line-height: 28px;
padding: 0px;
margin: 0px 2px 0px 2px;
font-size: 22px;
z-index: 0;
box-shadow: 3px 3px 5px #888888;
}
.calc-btn:visited,
.calc-btn:hover,
.calc-btn:active {
color: white !important;
}
.calc-btn-textsmall {
font-size: 16px;
}
.calc-btn-small {
color: #fff;
font-family: "verdana";
text-align: center;
background-color: rgb(66, 72, 104);
width: 36px;
height: 23px;
border-radius: 8px 8px 10px 10px;
line-height: 20px;
padding: 0px;
margin: 11.5px 2.1px 0px 2.1px;
font-size: 14px;
box-shadow: 2px 2px 5px #888888;
}
.calc-btn-small:visited,
.calc-btn-small:hover,
.calc-btn-small:active {
color: white !important;
}
.calc-btn-small-textsmall {
font-size: 12px;
}
.calc-btn-small-top-markings {
height: 11.5px;
line-height: 10px;
padding: 0px;
margin: 0px;
font-size: 1px;
background-color:black;
}
.calc-btn-small-alt-left {
display: inline-block;
width: 18.5px;
margin: 0px 0px 0px 4px;
padding: 0px;
text-align: left;
color: rgb(158, 152, 55);
font-weight: bold;
background-color: red;
}
.calc-btn-small-alt-right {
display: inline-block;
width: 18px;
margin: 0px 4px 0px 0px;
padding: 0px;
text-align: right;
color: rgb(175, 90, 119);
font-weight: bold;
background-color: blue;
}
.calc-btn-small-alt-center {
display: inline-block;
width: 36.5px;
margin: 0px 1px 0px 1px;
padding: 0px;
text-align: center;
color: rgb(158, 152, 55);
font-weight: bold;
background-color:black;
}
.cal-btn-grey {
background-color: rgb(110, 118, 121);
}
.cal-btn-purple {
background-color: rgb(99, 98, 173);
}
.cal-btn-orange {
background-color: rgb(182, 173, 103);
}
.cal-btn-red {
background-color: rgb(175, 84, 115);
}
#myModalLabel
{
font-family: Impact, Charcoal, sans-serif;
font-size: 18px;
}
$( window ).resize(function() {
$( "#calculator" ).attr("zoom") ;
});
// Global variables to store calculator settings
var calculator =
{
previousAnswer: 0,
justCalculated: false,
degreeOrRadians: "degree",
}
// This function append the supplied argument to the calculator display screen
function appendCalcScreen(inputString)
{
$("textarea.calc-screen").val($("textarea.calc-screen").val() + inputString);
}
function backspaceCalcScreen()
{
var currentCalcScreenValue = $("textarea.calc-screen").val();
if (currentCalcScreenValue.length > 0)
{
$("textarea.calc-screen").val(currentCalcScreenValue.substring(0, currentCalcScreenValue.length - 1));
}
}
function clearScreen()
{
$("textarea.calc-screen").val("");
}
// Defines the tokens and their properties.
var nonNumericTokensDefinitions =
[
{lexeme: "+", category:"operator", associativity:"left", precedence:1, evalFunction: (function(LHS, RHS) {return LHS+RHS;})},
{lexeme: "-", category:"operator", associativity:"left", precedence:1, evalFunction: (function(LHS, RHS) {return LHS-RHS;})},
{lexeme: "*", category:"operator", associativity:"left", precedence:2, evalFunction: (function(LHS, RHS) {return LHS*RHS;})},
{lexeme: "/", category:"operator", associativity:"left", precedence:2, evalFunction: (function(LHS, RHS) {return LHS/RHS;})},
{lexeme: "^", category:"operator", associativity:"right", precedence:3, evalFunction: (function(LHS, RHS) {return Math.pow(LHS,RHS);})},
{lexeme: "-u", category:"function", evalFunction: (function(RHS) {return -1.0*RHS;})},
{lexeme: "=", category:"operator", associativity:"right", precedence:0},
{lexeme: "log", category:"function", evalFunction: (function(RHS) {return Math.log10(RHS);})},
{lexeme: "ln", category:"function", evalFunction: (function(RHS) {return Math.log(RHS);})},
{lexeme: "sin", category:"function", evalFunction: (function(RHS) {return Math.sin(RHS);})},
{lexeme: "asin", category:"function", evalFunction: (function(RHS) {return Math.asin(RHS);})},
{lexeme: "cos", category:"function", evalFunction: (function(RHS) {return Math.cos(RHS);})},
{lexeme: "acos", category:"function", evalFunction: (function(RHS) {return Math.acos(RHS);})},
{lexeme: "tan", category:"function", evalFunction: (function(RHS) {return Math.tan(RHS);})},
{lexeme: "atan", category:"function", evalFunction: (function(RHS) {return Math.atan(RHS);})},
{lexeme: "Ans", category:"variable", evalFunction: (function() {return calculator.previousAnswer;})},
{lexeme: "(", category:"bracket"},
{lexeme: ")", category:"bracket"}
];
function evaulatePostfixTokens(tokenPostfixArray)
{
var i = 0;
while ((i < tokenPostfixArray.length) && (tokenPostfixArray.length !== 1))
{
var currentPostfixToken = tokenPostfixArray[i];
var currentPostfixTokenCategory = getTokenCategory(currentPostfixToken);
if ($.isNumeric(currentPostfixToken))
{
i++;
continue;
}
else if (currentPostfixTokenCategory === "function")
{
// If the function is at the start of the Postfix Token and has no parameter, flag n error
if (i < 1)
{
return null;
}
else if (($.isNumeric(tokenPostfixArray[i-1])) === true)
{
previousPostfixToken = tokenPostfixArray[i-1];
var functionEvaulationResult = getTokenEvalFunction(currentPostfixToken)(previousPostfixToken);
tokenPostfixArray.splice(i-1,2,functionEvaulationResult);
i = i - 1;
// Deal with the case when user specify something like 5log4, where there is an implicit *
if (($.isNumeric(tokenPostfixArray[i-1])) === true)
{
tokenPostfixArray.splice(i-1,2,tokenPostfixArray[i-1]*functionEvaulationResult);
i = i - 1;
}
}
else
{
return null;
}
}
else if (currentPostfixTokenCategory === "operator")
{
// Check if there is sufficient number of operands
if (i < 2)
{
// Insufficient number of operands, return error
return null;
}
// Check if the two items in front of the operator are numbers
else if ((($.isNumeric(tokenPostfixArray[i-1])) === true) && (($.isNumeric(tokenPostfixArray[i-2])) === true))
{
// Evaulate the operator
var functionEvaulationResult = getTokenEvalFunction(currentPostfixToken)(tokenPostfixArray[i-2],tokenPostfixArray[i-1]);
// Replace the operator and operands with the result
tokenPostfixArray.splice(i-2,3,functionEvaulationResult);
// Shift our scanner location
i = i - 2;
}
else
{
return null
}
}
}
// Deal with input where user specify 5(4*2) where there is an implicit * before brackets
if (($.isNumeric(tokenPostfixArray[i-1])) === true)
{
tokenPostfixArray.splice(i-2,2,tokenPostfixArray[i-1]*tokenPostfixArray[i-2]);
i = i - 2;
}
return tokenPostfixArray[0];
}
function getTokenProperty(inputLexeme, property)
{
for (var i = 0; i < nonNumericTokensDefinitions.length; i++)
{
if (nonNumericTokensDefinitions[i].lexeme === inputLexeme)
{
return nonNumericTokensDefinitions[i][property];
}
}
return null;
}
function getTokenEvalFunction(inputLexeme)
{
return getTokenProperty(inputLexeme, "evalFunction");
}
function getTokenPrecedence(inputLexeme)
{
return getTokenProperty(inputLexeme, "precedence");
}
function getTokenAssociativity(inputLexeme)
{
return getTokenProperty(inputLexeme, "associativity");
}
function getTokenCategory(inputLexeme)
{
return getTokenProperty(inputLexeme, "category");
}
function identifyNonNumericToken(inputStringArgument)
{
var matchedTokenLexeme = null;
// Loop through the tokens definition to find a match
for (var i = 0; i < nonNumericTokensDefinitions.length; i++)
{
currentTokenDefinition = nonNumericTokensDefinitions[i];
// See if our token matches our provided string
if (currentTokenDefinition.lexeme === inputStringArgument.substring(0,currentTokenDefinition.lexeme.length))
{
matchedTokenLexeme = currentTokenDefinition.lexeme;
return matchedTokenLexeme;
}
}
return matchedTokenLexeme;
}
// This function goes through a token array and identify any unery operators and provide a "u" postfix for identification
function processUneryOperators(inputTokenArray)
{
for (var i = 0; i < inputTokenArray.length; i++)
{
if (inputTokenArray[i] === "Ans")
{
inputTokenArray[i] = calculator.previousAnswer;
}
// Identify and flag unery "-" operator
if (inputTokenArray[i] === "-")
{
var previousToken = inputTokenArray[i-1];
var nextToken = inputTokenArray[i+1];
if (
(i === 0) ||
(getTokenCategory(previousToken) === "operator") ||
(previousToken == "(") ||
(getTokenCategory(nextToken) === "function")
)
{
inputTokenArray[i] = "-u";
}
}
}
return inputTokenArray;
}
function tokenize(inputStringArgument)
{
// Array of tokens that are to be returned
var tokenArray = [];
// Temporary placeholder for the current token as we parse the string
var currentToken ="";
// Remove unwanted whitespace
var inputString = inputStringArgument.replace(" ","");
console.log("Input String: " + inputString);
// Start parsing our string from left to right
for (var i = 0; i < inputString.length; i++)
{
// Test for numeric and decimals
if (($.isNumeric(inputString[i])) || (inputString[i] === "."))
{
// If the current token (so far) is numeric, we append this char to it
if ($.isNumeric(currentToken) || (currentToken === ""))
{
currentToken += inputString[i];
}
}
// Test the non-numeric value to match operators and functions
else
{
// We have found a non-numeric value, we will push any numeric token to the tokenArray
if ($.isNumeric(currentToken))
{
// Push into the tokenArray as a number (converted from token string)
tokenArray.push(Number(currentToken));
currentToken = "";
}
// We now match this non-numeric value to a token lexeme
currentToken = identifyNonNumericToken(inputString.substring(i));
// If we have found a valid non-numeric token
if (currentToken !== null)
{
tokenArray.push(currentToken);
// Advance our scanner forward by the lexueme's length less 1 (due to the i++ in the loop)
i += currentToken.length - 1;
currentToken = "";
}
else
{
// We have not found a valid token, return error.
return null;
}
}
// If we are at the end of our input string, push the last token to the array if it's not empty (numeric literal)
if ((i === (inputString.length - 1)) && (currentToken !== ""))
{
$.isNumeric(currentToken) ? tokenArray.push(Number(currentToken)) : tokenArray.push(currentToken);
}
}
return tokenArray;
}
function convertInfixTokenArraytoPostFix(infixTokenArray)
{
var postfixResult =[];
var operatorStack=[];
for (var i =0; i < infixTokenArray.length; i++)
{
currentToken = infixTokenArray[i];
if ($.isNumeric(currentToken))
{
postfixResult.push(currentToken);
// Check if the top token of the opstack is a function, if so, pop it into the postfix result
while (operatorStack.length >0)
{
operatorStackTopToken = operatorStack[operatorStack.length - 1];
if (getTokenCategory(operatorStackTopToken) === "function")
{
postfixResult.push(operatorStack.pop());
}
else
{
break;
}
}
}
else if (getTokenCategory(currentToken) === "variable")
{
operatorStack.push(currentToken);
}
else if (getTokenCategory(currentToken) === "function")
{
operatorStack.push(currentToken);
}
else if (getTokenCategory(currentToken) == "operator")
{
while (operatorStack.length > 0)
{
operatorStackTopToken = operatorStack[operatorStack.length - 1];
if (getTokenCategory(operatorStackTopToken) === "operator")
{
var opStackTopTokenAssociativity = getTokenAssociativity(operatorStackTopToken);
var opStackTopTokenPrecedence = getTokenPrecedence(operatorStackTopToken);
var currentTokenAssociativity = getTokenAssociativity(currentToken);
var currentTokenPrecedence = getTokenPrecedence(currentToken);
if (
((currentTokenAssociativity === "left") && (currentTokenPrecedence <= opStackTopTokenPrecedence))
||
((currentTokenAssociativity === "right") && (currentTokenPrecedence < opStackTopTokenPrecedence)))
{
postfixResult.push(operatorStack.pop());
}
else
{
break;
}
}
else
{
break;
}
}
operatorStack.push(currentToken);
}
else if(currentToken === "(")
{
operatorStack.push(currentToken);
}
else if(currentToken === ")")
{
do
{
operatorStackTopToken = operatorStack[operatorStack.length - 1];
if (operatorStackTopToken !== "(")
{
postfixResult.push(operatorStack.pop());
}
else
{
operatorStack.pop();
// Check if it's a pair of bracket enclosed for a function ie. cos(1+2), if so, pop the opstack into the postfix result
while (operatorStack.length >0)
{
operatorStackTopToken = operatorStack[operatorStack.length - 1];
if (getTokenCategory(operatorStackTopToken) === "function")
{
postfixResult.push(operatorStack.pop());
}
else
{
break;
}
}
break;
}
} while (operatorStack.length > 0)
}
}
while (operatorStack.length > 0)
{
postfixResult.push(operatorStack.pop());
}
return postfixResult;
}
function executeCalculate()
{
var processedTokenArray = tokenize($("textarea.calc-screen").val());
console.log(processedTokenArray);
processedTokenArray = processUneryOperators(processedTokenArray);
console.log(processedTokenArray);
processedTokenArray = convertInfixTokenArraytoPostFix(processedTokenArray);
console.log(processedTokenArray);
result = evaulatePostfixTokens(processedTokenArray);
console.log(result);
calculator.justCalculated = true;
// Write the result to screen, or shows "Error" if any error occured.
if (result !== null)
{
$("textarea.calc-screen").val(result);
calculator.previousAnswer = result;
}
else
{
$("textarea.calc-screen").val("Error");
}
}
$(document).ready(
function()
{
}
);
//////////////////////////////
// Capture keyboard events. //
//////////////////////////////
// Keydown function to capture "special" keys
$(document).keydown(function(){
// Key that applies even when user has the calculator screen TextArea focused.
//console.log(event.which);
switch (event.which)
{
case 13:
executeCalculate();
event.preventDefault();
break;
case 27:
$("[ccmd=AC]").trigger("click");
break;
}
// If user doesn't have the calculator screen TextArea focused, we want to capture the keyboard events still
if (($(document.activeElement)).attr("id") !== "calc-screen")
{
//console.log(event.which);
// Intercept certain keystrokes to link to special functions:
switch (event.which)
{
// User pressed "Enter", trigger the execute button
case 13:
executeCalculate();
event.preventDefault();
//backspaceCalcScreen();
break;
// User pressed "Shift"
case 16:
break;
// User pressed "Escape", press the AC button to clear screen
case 27:
$("[ccmd=AC]").trigger("click");
break;
// User pressed "Backspace", press the DEL button
case 8:
event.preventDefault();
$("[ccmd=DEL]").trigger("click");
break;
}
}
});
// Keypress function for all other "standard" keys
$(document).keypress(function(){
// If user doesn't have the calculator screen TextArea focused, we want to capture the keyboard events still
if (($(document.activeElement)).attr("id") !== "calc-screen")
{
if (calculator.justCalculated === true)
{
calculator.justCalculated = false;
clearScreen();
}
appendCalcScreen(String.fromCharCode(event.which));
}
});
function appendAnsIfRequired()
{
if (calculator.justCalculated === true)
{
calculator.justCalculated = false;
clearScreen();
appendCalcScreen("Ans");
}
return;
}
function clearScreenIfRequired()
{
if (calculator.justCalculated === true)
{
calculator.justCalculated = false;
clearScreen();
}
return;
}
// Event to convert clicks on buttons to their correct functions
$(".calc-btn, .calc-btn-small").click(function(){
var ButtonCommand = $(this).attr("ccmd");
switch (ButtonCommand)
{
// User pressed all clear to clear screen
case "AC":
clearScreen();
break;
// Call the parser to evaulate what's on screen
case "EXECUTE":
executeCalculate();
break;
case "DEL":
backspaceCalcScreen();
break;
case "ADD":
appendAnsIfRequired();
appendCalcScreen("+");
break;
case "MINUS":
appendAnsIfRequired();
appendCalcScreen("-");
break;
case "MULTIPLY":
appendAnsIfRequired();
appendCalcScreen("*");
break;
case "DIVIDE":
appendAnsIfRequired();
appendCalcScreen("/");
break;
case "SQUARED":
appendAnsIfRequired();
appendCalcScreen("^2");
break;
case "POWER":
appendAnsIfRequired();
appendCalcScreen("^");
break;
case "DECIMAL POINT":
appendCalcScreen(".");
break;
// Exponent in scientific notation
case "EXPONENT":
appendAnsIfRequired();
appendCalcScreen("*10^");
break;
case "UNERY NEGATIVE":
clearScreenIfRequired();
appendCalcScreen("(-1)*");
break;
case "FIT":
// window.open(this.href,"_self","width=290, height=520, location=no, menubar=no, status=no, toolbar=no");
window.open(this.href, "Calculator", "width=320, height=520");
break;
case undefined:
clearScreenIfRequired();
appendCalcScreen($(this).html());
}
});
Also see: Tab Triggers