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.
<input type="button" value="=" id="equals" class="noDisplay" onclick="equals()">
<!-- Additional Hidden Equals Button for Test Pass (non-functional) -->
<h1 id="heading">Javascript Calculator</h1>
<div class="calculator">
<input type="text" id="display" class="calculator-screen" value="0">
<div class="calculator-keys">
<input type="button" class="operator" value="+" id="add" onclick="pushToScreen('+')">
<!-- Add Button -->
<input type="button" value="-" id="subtract" class="operator" onclick="pushToScreen('-')">
<!-- Subtract Button -->
<input type="button" value="×" id="multiply" class="operator" onclick="pushToScreen('*')">
<!-- Multiply Button -->
<input type="button" value="÷" id="divide" class="operator" onclick="pushToScreen('/')">
<!-- Divide Button -->
<input type="button" value="7" id="seven" onclick="pushToScreen('7')">
<input type="button" value="8" id="eight" onclick="pushToScreen('8')">
<input type="button" value="9" id="nine" onclick="pushToScreen('9')">
<input type="button" value="4" id="four" onclick="pushToScreen('4')">
<input type="button" value="5" id="five" onclick="pushToScreen('5')">
<input type="button" value="6" id="six" onclick="pushToScreen('6')">
<input type="button" value="1" id="one" onclick="pushToScreen('1')">
<input type="button" value="2" id="two" onclick="pushToScreen('2')">
<input type="button" value="3" id="three" onclick="pushToScreen('3')">
<input type="button" value="0" id="zero" onclick="pushToScreen('0')">
<input type="button" value="." class="decimal" id="decimal" onclick="pushToScreen('.')">
<!-- Decimal Button -->
<input type="button" value="Clear" class="all-clear" id="clear"onclick="pushToScreen('c')">
<!-- Clear Button -->
<input type="button" value="=" id="equalsStyle" class="equal-sign" onclick="equals()">
<!-- Equals Button -->
</div>
</input>
<h2> here is my calculator!!!</h2>
html {
font-size: 62.5%;
box-sizing: border-box;
}
h1 {
font-size: 40px;
text-align:center;
}
h2 {position:absolute;
font-size:20px;
top:580px;
left:400px;
}
.noDisplay {display: none !important;}
*, *::before, *::after {
margin: 0;
padding: 0;
box-sizing: inherit;
}
.calculator {
border: 1px solid #ccc;
border-radius: 5px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 400px;
}
.calculator-screen {
width: 100%;
font-size: 5rem;
height: 80px;
border: none;
background-color: #eaeaea !important;
color: #fff;
text-align: right;
padding-right: 20px;
padding-left: 10px;
}
.calculator-screen:hover{cursor:default;}
input {
height: 60px;
background-color: #fff;
border-radius: 3px;
border: 1px solid #c4c4c4;
background-color: transparent;
font-size: 2rem;
color: #333;
background-image: linear-gradient(to bottom,transparent,transparent 50%,rgba(0,0,0,.04));
box-shadow: inset 0 0 0 1px rgba(255,255,255,.05), inset 0 1px 0 0 rgba(255,255,255,.45), inset 0 -1px 0 0 rgba(255,255,255,.15), 0 1px 0 0 rgba(255,255,255,.15);
text-shadow: 0 1px rgba(255,255,255,.4);
}
input:hover {
background-color: #eaeaea;
cursor: pointer;
}
.operator {
color: #337cac;
}
.all-clear {
background-color: #f0595f;
border-color: #b0353a;
color: #fff;
}
.all-clear:hover {
background-color: #f17377;
}
.equal-sign {
background-color: #2e86c0;
border-color: #337cac;
color: #fff;
height: 100%;
grid-area: 2 / 4 / 6 / 5;
}
.equal-sign:hover {
background-color: #4e9ed4;
}
.calculator-keys {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-gap: 20px;
padding: 20px;
}
// I really like you calculator styling, nice work!! And keep going, so close and good luck!
var answer = document.getElementById("display");
var prevKeys = [];// track each key pressed. It's one button event behind what's displayed in the calculator. Do a console.log(prevKeys) to see.
var numberHistory = []; //used to decide if a complete number was created, if this array length is greater than 0, we can add another decimal to the formula. the array is cleared after a decimal is added.
function pushToScreen(x) {
var currentKey = x; //new variable named for readability
var isOperator = /[+\*-/]/g; //regex that's suppose to test for operator keys, but decimal seems to pass as well.
var prevKeysStr = prevKeys.join('').toString(); //the values in the input are ahead by one event, so we keep an array of keys press and convert to a string so we can use substring() or includes()
var lastKeyIsOperator = isOperator.test(currentKey); // used to test the last key pressed and see if it's an operator. Decimals still pass for some reason, not sure why.
var formulaHasOperatorBeforeEnd = isOperator.test(prevKeysStr.substring(prevKeysStr.length -1, prevKeysStr.length)[0]); // test the last key value in our history (not including current key pressed) is an operator, **reminder** decimals seem to pass
if (isOperator.test(x) && (x !== ".")) { //handle operators here
numberHistory.push(prevKeysStr); //if an operator was pressed, we add the numbers before it to our array. allowing us to add a decimal again
//needs more work here to finish up all tests, so close!
// ultimately you want to allow a negative operator after any one other operator, but replace all operators with the newest operator pressed without allowing duplicate operators back-to-back. One way could be to look at prevKey history and use subtring to remove the last one or two operators and combine with the newest operator/currentKey
if (formulaHasOperatorBeforeEnd && lastKeyIsOperator && currentKey !== "-") {
answer.value = prevKeysStr.substring(0, prevKeysStr.length -2) + currentKey;
prevKeys.push(currentKey);
} else {
answer.value = answer.value + currentKey;
prevKeys.push(currentKey);
}
} else if (x === "c") { //handle clearing button here
answer.value = "0";
prevKeys = [];
} else if ((x !== "c") && (x !== ".") && (answer.value === "0")) { //handle first number key pressed after clear
answer.value = x;
prevKeys = [];
prevKeys.push(x);
} else if (x === ".") { // handle decimal behaviors here
if (!prevKeysStr.includes(".")) { //if no decimals exist in previous button presses
prevKeys.push(x);
answer.value += x;
} else if ( //if the last item in previous keys is one of the operators and not a decimal
prevKeysStr.substring(prevKeysStr.length -1, prevKeysStr.length) !== '.' &&
( prevKeysStr.substring(prevKeysStr.length -1, prevKeysStr.length) == '+' ||
prevKeysStr.substring(prevKeysStr.length -1, prevKeysStr.length) == '/' ||
prevKeysStr.substring(prevKeysStr.length -1, prevKeysStr.length) == '-' ||
prevKeysStr.substring(prevKeysStr.length -1, prevKeysStr.length) == '*')
) {
prevKeys.push(x);
answer.value += x;
} else if ( // if there's a number in our history, added by pressing an operator, and we didn't have a decimal at the end of all previous keys pressed, we can add a decimal again. then we clear the last number from our history
numberHistory.length > 0 &&
prevKeysStr.substring(prevKeysStr.length -1, prevKeysStr.length) !== '.'
) {
prevKeys.push(x);
answer.value += x;
numberHistory = []; //clear number history
}
} else { //normal display updating here
answer.value += x;
prevKeys.push(x);
}
//updates display input in HTML (not necessary/important)
answer.setAttribute('value', answer.value);
}
function equals() {
numberHistory = []; //clear number history
x = answer.value;
x = eval(x);
answer.value = x;
}
Also see: Tab Triggers