Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

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.

Behavior

Auto Save

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

              
                <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css"> 
<div id="root"></div>

              
            
!

CSS

              
                :root {
  --calc-base: #4E5B5E;
  --screen-holder: #A3BE7D;
  --screen-color: #CDCF9D;
  --calc-yellow: #DEE036;
  --calc-red: #C4467C;
  --calc-black: #292929;
  --calc-grey: #E3E3E3;
  --button-press-border: #6E6E6E;
  --overlay-gradient: radial-gradient(rgba(0, 0, 0, 0) 50%, rgba(0, 0, 0, .3));
}



/*-- Classes --*/
.calc-button {
  width: 66px;
  height: 60px;
  margin: 10px;
  border-radius: 10px;
  cursor: pointer;
  background: var(--calc-grey) var(--overlay-gradient);
  color: var(--calc-black);
}

.calc-button:active {
  box-shadow: inset 5px 5px 5px black;
  border: 2px solid var(--button-press-border);
}

.wide-button:active, .tall-button:active {
  box-shadow: inset 5px 5px 5px black;
  border: 2px solid var(--button-press-border);
}

.wide-button {
  height: 60px;
  margin: 10px;
  border-radius: 10px;
  width: 156px;
  background: var(--calc-yellow) var(--overlay-gradient);
  cursor: pointer;
  color: var(--calc-black);
}

.tall-button {
  height: 140px;
  margin: 10px;
  border-radius: 10px;
  width: 66px;
  background: var(--calc-yellow) var(--overlay-gradient);
  cursor: pointer;
}

.calc-button, .wide-button, .tall-button {
  box-shadow: 3px 3px 5px black;
  font-size: 30px;
  border: none;
}

.red-calc {
  background: var(--calc-red) var(--overlay-gradient);
  color: var(--calc-grey);
}

.black-calc {
  background: var(--calc-black) var(--overlay-gradient);
  color: var(--calc-grey);
}

.ac {
  font-size: 20px;
}

.calc-button {
  margin: .3em;
}
/*-- (close) Classes --*/


/*-- ID's --*/
#calculator-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 5rem;
}

#calculator {
  min-width: 400px;
  height: 600px;
  background: var(--calc-base) linear-gradient(90deg, rgba(0, 0, 0, .4), rgba(0, 0, 0, .01) 3%, rgba(0, 0, 0, .01) 98%, rgba(0, 0, 0, .4) 100%);
  border-radius: 20px;
  position: relative;
  box-shadow: 10px 10px 10px black;
}

#display-container {
  width: 380px;
  height: 100px;
  background: var(--screen-holder);
  position: absolute;
  left: 10px;
  top: 10px;
  border-radius: 12px 12px 5px 5px;
  box-shadow: inset 2px 2px 2px black;
}

#display-box {
  height: 50px;
  background: var(--screen-color);
  box-shadow: inset 3px 3px 5px black;
  position: absolute;
  width: 90%;
  left: 5%;
  top: 40px;
}

#display {
  text-align: right;
  margin-right: 10px;
  margin-top: 11px;
  font-size: 30px;
  font-family: OCR A Std, monospace	;
}

#button-bank {
  height: 400px;
  width: 90%;
  margin: 5%;
  position: absolute;
  bottom: 5px;
}

#mid-panel {
  width: 340px;
  height: 43px;
  position: absolute;
  margin-top: 120px;
  margin-left: 25px;
}

#solar-slot {
  width: 140px;
  height: 43px;
  background: linear-gradient(120deg, white, #999999 25%, #999999 75%, white);
  border-radius: 10px;
  box-shadow: inset 1px 1px 2px black;
  display: relative;
}

#solar-cell {
  background: #6B6145;
  height: 34px;
  width: 110px;
  margin-left: 15px;
  margin-top: 4px;
  display: inline-block;
  display: absolute;
  box-shadow: inset 3px 3px 5px black;
  font-family: sans-serif;
  font-size: 32px;
  text-align: center;
  letter-spacing: 2.8px;
  color: #968268;
}

#text-elements {
  font-family: OCR A Std, monospace	;
  color: white;
  position: absolute;
  right: 25px;
  top: 118px;
}

#digit {
  position: absolute;
  top: 18px;
  left: 85px;
  font-size: 30px;
  color: var(--calc-yellow);
}



#calculator-btns {
  position: absolute;
  bottom: 1em;
  left: 1.8em;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(5, 1fr);
  grid-column-gap: 0px;
  grid-row-gap: 0px;
  margin: auto;
}

#clear, #divide, #multiply {
  grid-row-start: 1;
}

#clear {
  grid-column-start: 1;
  grid-column-end: 3;
}

#seven, #eight, #nine, #subtract {
  grid-row-start: 2;
}

#four, #five, #six, #add {
  grid-row-start: 3;
} 

#one, #two, #three, #equals {
  grid-row-start: 4;
}

#equals {
  grid-row-end: 6;
}

#zero, #period {
  grid-row-start: 5;
}

#zero {
  grid-column-start: 1;
  grid-column-end: 3;
}

/*-- (close) ID's --*/
              
            
!

JS

              
                import React from "https://esm.sh/react"
import ReactDOM from "https://esm.sh/react-dom"


class DecorativeElements extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    return (
      <>
        <div id="mid-panel">
          <div id="solar-slot">
            <div id="solar-cell">l l l l</div>
          </div>
        </div>
      <div id="text-elements">Jay's Calculator<span id="digit">5WA6</span></div>
      </>
    )
  }
}

//This component renders the display, the text that occupies the display is passed as props.
class Display extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
  return (
    <>
      <div id="display-container">
      <div id="display-box">
      <div id="display">{this.props.displayText}</div>
      </div>
      </div>
    </>
  )
}
}

//This class renders the generic calculator button and functionality. It's modified using CSS for the buttons of different styles.


class CalculatorButton extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    return (
      
      <button className={this.props.classProp} id={this.props.propID} onClick={this.props.handleClick}>{this.props.buttonType}</button>
    )
  }
}


//This is the main body of the app. 
class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      textOnDisplay: 'Change This',
      equation: [],
      numberInEquation: '0',
      result: ''
    }
    this.handleClick = this.handleClick.bind(this);
    this.createEquation = this.createEquation.bind(this);
    this.performCalculation = this.performCalculation.bind(this);
    this.simplifyEquation = this.simplifyEquation.bind(this);
  }

//Keeping the button's handleClick very simple. The parameter 'button' is passed into the method as a string or integer. That value will be assessed in this.createEquation.
handleClick(button) {
  this.createEquation(button);
}
  
//simplifyEquation goes through the inputted equation AFTER the equals button has been pressed. simplifyEquation's job is to remove any extraneous operators. In between each value in the equation, it determines how many operators there are. If there is one operator, that will be pushed into tempEquationArray. If there is more than one operator, it will push the most recent operator(s) into tempEquationArray. It will only push more than one operator into tempEquationArray if the oeprator closest to the next value is a '-' (negative) operator.
//simplifyEquation returns a string of the equation to be evaulated in this.performCalculation.
simplifyEquation(equationString) {
  let tempEquationArray = [];
  let countOperators = 0;
  for (let i = 0; i < equationString.length; i++) {
    if (Number.parseInt(equationString[i]) || equationString[i] === '.' || equationString[i] === '0') {
         
        if (countOperators === 1) {
          tempEquationArray.push(equationString[i-1]);
          countOperators = 0;
        } else if (countOperators > 1 && equationString[i - 1] === '-') {
          tempEquationArray.push(equationString[i - 2], equationString[i - 1]);
          countOperators = 0;
        } else if (countOperators >= 2){
          tempEquationArray.push(equationString[i-1]);
          console.log(tempEquationArray, "here")
          countOperators = 0;
        }
        if (Number.parseInt(equationString[i]) || equationString[i] === '.' || equationString[i] === '0') {
          tempEquationArray.push(equationString[i]);
        }
    } else {
      countOperators++;
    }
  }
  return tempEquationArray.join('');
}
  
//When a user presses 'equals', this.performCalculation is called. performCalculation assigns the value of a this.simplifyEquation call to simplifiedEquation and then evaluates that equation.
performCalculation(equationArray) {
  if (this.state.equation.length === 0) {
    this.state.setState({
      equation: [],
      numberInEquation: ''
    })
  }  else {
    let equationString = equationArray.join('')   
    let simplifiedEquation = this.simplifyEquation(equationString);
    console.log(simplifiedEquation)
    let result = eval(simplifiedEquation).toString();
    
    this.setState({
      result: result
    })
    return result;    
  }
}
  
//this.createEquation constructs an array of numbers from the buttons that a user presses. this.performCalculation is called when a used presses the 'equals' button.
createEquation(button) {
  if (Number.isInteger(parseInt(button))) {
        if (this.state.numberInEquation.length === 1 && this.state.numberInEquation ==='0') {
          this.setState({
            numberInEquation: button.toString()
          })   
        } else if (this.state.numberInEquation.length >= 1 || this.state.numberInEquation === '') {
          this.setState({
          numberInEquation: this.state.numberInEquation + button.toString()
          })
    }
  } else {
    
    if (button === 'clear') {
        return (
        this.setState((state, props) => ({
          equation: [],
          numberInEquation: '0',
          result: ''
        }))
        )
    }
    
     if (button === 'add') {
        let possibleInteger = Number.isInteger(parseInt(this.state.numberInEquation))
        let newEquation;
        if (possibleInteger) {
          newEquation = this.state.equation.concat(this.state.numberInEquation).concat('+')
        } else {
          newEquation = this.state.equation.concat('+');
        }

        return (
        this.setState((state, props) => ({
          equation: newEquation,
          numberInEquation: ''
        }))
        )
    }
    
     if (button === 'subtract') {
        let possibleInteger = Number.isInteger(parseInt(this.state.numberInEquation))
        let newEquation;
        if (possibleInteger) {
          newEquation = this.state.equation.concat(this.state.numberInEquation).concat('-')
        } else {
          newEquation = this.state.equation.concat('-');
        }

        return (
        this.setState((state, props) => ({
          equation: newEquation,
          numberInEquation: ''
        }))
        )
    }
    
     if (button === 'divide') {
        let possibleInteger = Number.isInteger(parseInt(this.state.numberInEquation))
        let newEquation;
        if (possibleInteger) {
          newEquation = this.state.equation.concat(this.state.numberInEquation).concat('/')
        } else {
          newEquation = this.state.equation.concat('/');
        }

        return (
        this.setState((state, props) => ({
          equation: newEquation,
          numberInEquation: ''
        }))
        )
    }
    
     if (button === 'decimal') {
        let newNumber = this.state.numberInEquation;
        if (newNumber.match(/[.]/g)) {
           return null;
        } else {
          if (newNumber === '') {
            newNumber = this.state.numberInEquation + '0.';
          } else {
            newNumber = this.state.numberInEquation + '.';
          }
        }
        return (
        this.setState((state, props) => ({
          numberInEquation: newNumber
        }))
        )
    }  
     if (button === 'multiply') {
        let possibleInteger = Number.isInteger(parseInt(this.state.numberInEquation))
        let newEquation;
        if (possibleInteger) {
          newEquation = this.state.equation.concat(this.state.numberInEquation).concat('*')
        } else {
          newEquation = this.state.equation.concat('*');
        }

        return (
        this.setState((state, props) => ({
          equation: newEquation,
          numberInEquation: ''
        }))
        )
    }
    
      if (button === 'equals') {
        let newEquation = this.state.equation.concat(this.state.numberInEquation);
        let newResult = [this.performCalculation(newEquation)];
        return (
        this.setState((state, props) => ({
          equation: newResult,
          numberInEquation: '',
        }))
        )
          return 0;
    }
  }
}
 

  render() {
    console.log(this.state)
    return (
      <div id="calculator-wrapper">
        <div id="calculator">
    <Display displayText={this.state.result.length > 0 ? this.state.result : this.state.numberInEquation}/>
    <DecorativeElements />
    <div id="calculator-btns">
          <CalculatorButton propID={"clear"} buttonType={"AC"} classProp={"wide-button ac"} handleClick={()=>{this.handleClick('clear')}}/>
          <CalculatorButton propID={"divide"} buttonType={"%"} classProp={"calc-button red-calc"} handleClick={()=>{this.handleClick('divide')}}/>
          <CalculatorButton propID={"multiply"}  buttonType={"X"} classProp={"calc-button red-calc"} handleClick={()=>{this.handleClick('multiply')}}/>
          <CalculatorButton propID={"seven"}  buttonType={"7"} classProp={"calc-button"} handleClick={()=>{this.handleClick(7)}}/>
          <CalculatorButton propID={"eight"}  buttonType={"8"} classProp={"calc-button"} handleClick={()=>{this.handleClick(8)}}/>
          <CalculatorButton propID={"nine"}  buttonType={"9"} classProp={"calc-button"} handleClick={()=>{this.handleClick(9)}}/>
          <CalculatorButton propID={"subtract"}  buttonType={"-"} classProp={"calc-button red-calc"} handleClick={()=>{this.handleClick('subtract')}}/>
          <CalculatorButton propID={"four"}  buttonType={"4"} classProp={"calc-button"} handleClick={()=>{this.handleClick(4)}}/>
          <CalculatorButton propID={"five"}  buttonType={"5"} classProp={"calc-button"} handleClick={()=>{this.handleClick(5)}}/>
          <CalculatorButton propID={"six"}  buttonType={"6"} classProp={"calc-button"} handleClick={()=>{this.handleClick(6)}}/>
          <CalculatorButton propID={"add"}  buttonType={"+"} classProp={"calc-button red-calc"} handleClick={()=>{this.handleClick('add')}}/>
          <CalculatorButton propID={"one"}  buttonType={"1"} classProp={"calc-button"} handleClick={()=>{this.handleClick(1)}}/>
          <CalculatorButton propID={"two"}  buttonType={"2"} classProp={"calc-button"} handleClick={()=>{this.handleClick(2)}}/>
          <CalculatorButton propID={"three"}  buttonType={"3"} classProp={"calc-button"} handleClick={()=>{this.handleClick(3)}}/>
          <CalculatorButton propID={"equals"}  buttonType={"="} classProp={"tall-button"} handleClick={()=>{this.handleClick('equals')}}/>
          <CalculatorButton propID={"zero"}  buttonType={"0"} classProp={"wide-button"} handleClick={()=>{this.handleClick(0)}}/>
          <CalculatorButton propID={"decimal"}  buttonType={"."} classProp={"black-calc calc-button"}  handleClick={()=>{this.handleClick('decimal')}}/>
          </div>
        </div>
      </div>
    )
  }
}

ReactDOM.render(<App />, document.getElementById('root'));
              
            
!
999px

Console