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 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.
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 esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM 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.
<div id="root"></div>
@import url("https://fonts.googleapis.com/css?family=Audiowide|Graduate&display=swap");
body {
margin: 0;
background-color: #d6d6c2;
}
#root {
width: 380px;
height: 500px;
margin: 50px auto;
background-color: #8a5c8a;
box-shadow: 0 0 5px 5px rgba(0, 0, 0, 0.2);
border-radius: 2%;
}
.windowDisplay {
font-family: "Graduate", sans-serif;
width: 100%;
height: 50px;
background-color: #220026;
color: #ff1a66;
text-align: right;
font-size: 22px;
line-height: 50px;
overflow: hidden;
white-space: nowrap;
direction: ltr;
}
#container {
padding: 30px 10px 15px 10px;
}
button {
width: 75px;
height: 50px;
background-color: #191919;
color: deeppink;
margin: 10px;
font-size: 20px;
font-family: "Audiowide", sans-serif;
border-radius: 15%;
}
.modify {
width: 75px;
height: 120px;
float: right;
background-color: #990000;
}
p {
text-align: center;
color: #800040;
font-size: 18px;
}
// REACT CALCULATOR, a personal project based on FCC assignment
class Display extends React.Component {
componentDidUpdate() {
// The width of the -windowDisplay- container is fixed at 360. Use of property scrollWidth of this element to 'move' its content to the left (scrollWidth - 360) units of distance (in pixels)
var myDiv = document.getElementsByClassName("windowDisplay")[0];
if (myDiv.scrollWidth > 360) {
myDiv.scrollLeft = myDiv.scrollWidth - 360;
}
}
render() {
return(
<div id="container">
<div id="display" className="windowDisplay">{this.props.displayOperation}</div>
</div>
)
}
}
class Calculator extends React.Component {
constructor(props) {
super(props);
this.state = {
buttonsPressed: '',
mathArray: [],
result: ''
}
this.handleNumbers = this.handleNumbers.bind(this);
this.handleReset = this.handleReset.bind(this);
this.handleDelete = this.handleDelete.bind(this);
this.doMath = this.doMath.bind(this);
this.handleOperator = this.handleOperator.bind(this);
this.handleDecimals = this.handleDecimals.bind(this);
}
handleNumbers(event) {
var targetButton = event.target.textContent;
// If math operation outcome is displayed, overwrite it with any new numeric input. Manage new number typing vs appending digits to current number within the math array. Set limit for number of digits (10)
if ((/\d+/).test(this.state.result)) {
this.setState({
buttonsPressed: targetButton,
mathArray: [...targetButton],
result: ''
});
} else if (this.state.buttonsPressed.length === 0 || (/[\D]$/).test(this.state.buttonsPressed) && this.state.buttonsPressed.slice(-1) !== "." && this.state.result === ''){
this.setState({
buttonsPressed: this.state.buttonsPressed + targetButton,
mathArray: [...this.state.mathArray, targetButton]
});
} else if ((/\d{10}$/).test(this.state.buttonsPressed) && targetButton !== ".") {
this.setState({
result: "DIGIT LIMIT EXCEEDED"
});
} else if (this.state.result === '') {
if(this.state.mathArray.slice(-1).toString() === "0") {
this.setState({
buttonsPressed: this.state.buttonsPressed.slice(0, -1) + targetButton,
mathArray: [...this.state.mathArray.slice(0, -1), targetButton]
});
} else {
this.setState({
buttonsPressed: this.state.buttonsPressed + targetButton,
mathArray: [...this.state.mathArray.slice(0, -1), this.state.mathArray.slice(-1).toString() + targetButton]
});
}
}
}
handleReset() {
// Button -reset- sets all anew, return to initial conditions (when first time rendered)
this.setState({
buttonsPressed: '',
mathArray: [],
result: ''
});
}
handleDelete() {
// Use of the -delete- button to come back to the previous math operation input, then compare the length of the last item in the Math array and decide wether to delete it whole or slice it
if(this.state.result !== ''){
this.setState({
result: ''
});
} else if (this.state.mathArray.slice(-1).toString().length > 1) {
this.setState({
buttonsPressed: this.state.buttonsPressed.slice(0, -1),
mathArray: [...this.state.mathArray.slice(0, -1), this.state.mathArray.slice(-1).toString().slice(0, -1)]
});
} else {
this.setState({
buttonsPressed: this.state.buttonsPressed.slice(0, -1),
mathArray: this.state.mathArray.slice(0, -1)
});
}
}
handleDecimals(event) {
var targetButton = event.target.textContent;
if((/\d+/).test(this.state.result)) {
this.setState({
buttonsPressed: targetButton,
mathArray: [...targetButton],
result: ''
});
} else if(this.state.buttonsPressed.length === 0 || (/[\D]$/).test(this.state.buttonsPressed) && this.state.buttonsPressed.slice(-1) !== "." && this.state.result === ''){
this.setState({
buttonsPressed: this.state.buttonsPressed + targetButton,
mathArray: [...this.state.mathArray, targetButton]
});
} else if (this.state.buttonsPressed.slice(-1) !== "." && this.state.result === '') {
this.setState({
buttonsPressed: this.state.buttonsPressed + targetButton,
mathArray: [...this.state.mathArray.slice(0, -1), this.state.mathArray.slice(-1).toString() + targetButton]
});
}
}
handleOperator(event) {
// If math operation outcome is displayed, use it as the new -string- & -item in math array- to write the next math exp. Otherwise, keep inserting '+' & '-'
var targetButton = event.target.textContent;
if((/\d+/).test(this.state.result)) {
this.setState({
buttonsPressed: this.state.result + targetButton,
mathArray: [this.state.result, targetButton],
result: ''
});
} else if (this.state.result === '' && (targetButton === "/" || targetButton === "x") && this.state.buttonsPressed.slice(-1) !== ".") {
if ((/[\d][\D]$/).test(this.state.buttonsPressed)) {
this.setState({
buttonsPressed: this.state.buttonsPressed.slice(0, -1) + targetButton,
mathArray: [...this.state.mathArray.slice(0, -1), targetButton]
});
} else if((/[\d]$/).test(this.state.buttonsPressed)) {
this.setState({
buttonsPressed: this.state.buttonsPressed + targetButton,
mathArray: [...this.state.mathArray, targetButton]
});
}
} else if (((/[\d][\D]$|[\d]$/).test(this.state.buttonsPressed) || this.state.buttonsPressed.length < 2) && this.state.result === '' && targetButton !== "x" && targetButton !== "/" && this.state.buttonsPressed.slice(-1) !== ".") {
this.setState({
buttonsPressed: this.state.buttonsPressed + targetButton,
mathArray: [...this.state.mathArray, targetButton]
});
}
}
doMath() {
// Use of the math array to evaluate input expression. -eval- function not used.
var resultArr = this.state.mathArray;
if((/[\D]$/).test(resultArr)) {
this.setState({
result: "Syntax Error"
});
} else if (resultArr.length > 0) {
// In the array number as strings will be converted to numbers, and operators as strings will be used as tokens to do one mathematical operation at a time. The result is inserted in the array where affected operands were. The loop continues until the array only contains one item: the result of the mathematical expression.
while (resultArr.length > 1) {
var calculate = function(operatorIndex, myArr) {
var operand1 = myArr[operatorIndex-1];
var operand2 = myArr[operatorIndex+1];
if(operand2 === "x" || operand2 === "-" || operand2 === "/" || operand2 === "+") {
operand2 = Number(myArr[operatorIndex+2]) * Number(operand2 + 1);
myArr.splice(operatorIndex+1, 2, operand2);
}
if (operatorIndex === 0) {
operand1 = 0;
}
var operationResult = 0;
switch(myArr[operatorIndex]){
case "x":
operationResult = Number(operand1) * Number(operand2);
break;
case "/":
operationResult = Number(operand1) / Number(operand2);
break;
case "+":
operationResult = Number(operand1) + Number(operand2);
break;
case "-":
operationResult = Number(operand1) - Number(operand2);
break;
}
if(operatorIndex === 0) {
myArr.splice(operatorIndex, 3, operationResult);
} else {
myArr.splice(operatorIndex-1, 3, operationResult);
}
}
var minusIndex = resultArr.indexOf("-");
var plusIndex = resultArr.indexOf("+");
var multiplyIndex = resultArr.indexOf("x");
var divideIndex = resultArr.indexOf("/");
if(multiplyIndex >= 0 || divideIndex >= 0) {
if(multiplyIndex !== -1) {
calculate(multiplyIndex, resultArr);
} else {
calculate(divideIndex, resultArr);
}
} else if (plusIndex >= 0 || minusIndex >= 0){
if ((Math.min(plusIndex, minusIndex) === plusIndex && plusIndex >= 0) || (plusIndex >= 0 && minusIndex < 0)) {
calculate(plusIndex, resultArr);
} else if ((Math.min(plusIndex, minusIndex) === minusIndex && minusIndex >= 0) || (minusIndex >= 0 && plusIndex < 0)) {
calculate(minusIndex, resultArr);
}
}
}
this.setState({
result: Math.round(1000000000000 * resultArr[0]) / 1000000000000
});
}
}
render() {
var message;
if(this.state.result !== '') {
message = <Display displayOperation={this.state.result} />;
} else {
message = <Display displayOperation={this.state.buttonsPressed} />;
}
return(
<div id="math-pad">
{message}
<button id="seven" onClick={this.handleNumbers}>7</button>
<button id="eight" onClick={this.handleNumbers}>8</button>
<button id="nine" onClick={this.handleNumbers}>9</button>
<button id="clear" className="modify" onClick={this.handleReset}>AC</button>
<button id="four" onClick={this.handleNumbers}>4</button>
<button id="five" onClick={this.handleNumbers}>5</button>
<button id="six" onClick={this.handleNumbers}>6</button>
<button id="delete" className="modify" onClick={this.handleDelete}>DEL</button>
<button id="one" onClick={this.handleNumbers}>1</button>
<button id="two" onClick={this.handleNumbers}>2</button>
<button id="three" onClick={this.handleNumbers}>3</button>
<button id="zero" onClick={this.handleNumbers}>0</button>
<button id="decimal" onClick={this.handleDecimals}>.</button>
<button id="add" onClick={this.handleOperator}>+</button>
<button id="multiply" onClick={this.handleOperator}>x</button>
<button id="substract" onClick={this.handleOperator}>-</button>
<button id="divide" onClick={this.handleOperator}>/</button>
<button id="equals" onClick={this.doMath}>=</button>
<p>CUBE Power®</p>
</div>
)
}
}
ReactDOM.render(<Calculator />, document.querySelector("#root"));
Also see: Tab Triggers