Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ 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

Save Automatically?

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

              
                <div id="root"></div>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css2?family=Codystar&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Dancing+Script&display=swap');
/*
* Prefixed by https://autoprefixer.github.io
* PostCSS: v7.0.29,
* Autoprefixer: v9.7.6
* Browsers: last 4 version
*/

* {
  margin:0;
  padding:0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

html {
  font-size: 10px;
  -webkit-box-sizing: border-box;
          box-sizing: border-box;
  margin:0;
}

*, *::before, *::after {
  -webkit-box-sizing: inherit;
          box-sizing: inherit;
  
}

body {
background-color: blue;
  font-family: 'Codystar', sans-serif;
  background-image: -o-linear-gradient(150deg, teal, orchid);
  background-image: linear-gradient(-60deg, teal, orchid)
}

#root {
  height: 100vh;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
      -ms-flex-direction: column;
          flex-direction: column;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
}

.calculator-body {
  max-width: 700px;
  min-width: 300px;
  height: auto;
  background-image: -o-linear-gradient(60deg, lightsteelblue 25%, white, lightsteelblue 60%);
  background-image: linear-gradient(30deg, lightsteelblue 25%, white, lightsteelblue 60%);
  border-radius: 3rem;
  padding: 3rem;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
      -ms-flex-direction: column;
          flex-direction: column;
  gap: 3rem;
  -webkit-box-shadow: inset -1px -1px 10px 5px rgba(0, 0, 0, 0.50);
          box-shadow: inset -1px -1px 10px 5px rgba(0, 0, 0, 0.50);
}


.display-panel {
  background-color: black;
  border-radius: 1rem;
  padding: 0.5rem;
  -webkit-box-shadow: 0px 0px 5px 2px rgba(0, 0, 0, 0.50);
          box-shadow: 0px 0px 5px 2px rgba(0, 0, 0, 0.50);
}

.screen {
  font-family: 'Codystar', sans-serif;
  width: 100%;
  text-align: right;
}
.input {
  color: red;
  font-size: 4rem;
}

.output {
  font-size: 2rem;
  color: white;
}

.button-panel {
  
  display: -ms-grid;
  
  display: grid;
  gap: .7rem;
      grid-template-areas:
    "clear clear divide multiply"
    "seven eight nine subtract"
    "four five six add"
    "one two three equals"
    "zero zero decimal equals";
}

.button {
  font-family: system-ui, sans-serif;
  font-size: 3rem;
  background-color: grey;
  padding: 1.5rem;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  border-radius: 1rem;
  outline:0;
  -webkit-box-shadow: inset -1px -1px 5px 2px rgba(0, 0, 0, 0.50), 0px 0px 2px 1px rgba(0, 0, 0, 0.5);
          box-shadow: inset -1px -1px 5px 2px rgba(0, 0, 0, 0.50), 0px 0px 2px 1px rgba(0, 0, 0, 0.5);
  -webkit-touch-callout: none; /* iOS Safari */
    -webkit-user-select: none; /* Safari */
     -khtml-user-select: none; /* Konqueror HTML */
       -moz-user-select: none; /* Old versions of Firefox */
        -ms-user-select: none; /* Internet Explorer/Edge */
            user-select: none; /* Non-prefixed version, currently
                                  supported by Chrome, Edge, Opera and Firefox */
  cursor: pointer;
  
}

.button:active {
  -webkit-box-shadow: inset -1px -1px 5px 2px rgba(0, 0, 0, 0.9), 0px 0px 2px 1px rgba(0, 0, 0, 0.5);
          box-shadow: inset -1px -1px 5px 2px rgba(0, 0, 0, 0.9), 0px 0px 2px 1px rgba(0, 0, 0, 0.5);
}

.number {
  background-color: slategray;
}

.operator {
  background-color: darkslategray;
}

.clear {
  background-color: red;
}

.equals {
  background-color: dodgerblue;
}


#zero {-ms-grid-row: 9;-ms-grid-column: 1;-ms-grid-column-span: 3;grid-area: zero;}
#one {-ms-grid-row: 7;-ms-grid-column: 1;grid-area: one;}
#two {-ms-grid-row: 7;-ms-grid-column: 3;grid-area: two;}
#three {-ms-grid-row: 7;-ms-grid-column: 5;grid-area: three;}
#four {-ms-grid-row: 5;-ms-grid-column: 1;grid-area: four;}
#five {-ms-grid-row: 5;-ms-grid-column: 3;grid-area: five;}
#six {-ms-grid-row: 5;-ms-grid-column: 5;grid-area: six;}
#seven {-ms-grid-row: 3;-ms-grid-column: 1;grid-area: seven;}
#eight {-ms-grid-row: 3;-ms-grid-column: 3;grid-area: eight;}
#nine {-ms-grid-row: 3;-ms-grid-column: 5;grid-area: nine;}
#decimal {-ms-grid-row: 9;-ms-grid-column: 5;grid-area: decimal;}
#equals {-ms-grid-row: 7;-ms-grid-row-span: 3;-ms-grid-column: 7;grid-area: equals;}
#add {-ms-grid-row: 5;-ms-grid-column: 7;grid-area: add;}
#subtract {-ms-grid-row: 3;-ms-grid-column: 7;grid-area: subtract;}
#multiply {-ms-grid-row: 1;-ms-grid-column: 7;grid-area: multiply;}
#divide {-ms-grid-row: 1;-ms-grid-column: 5;grid-area: divide;}
#clear {-ms-grid-row: 1;-ms-grid-column: 1;-ms-grid-column-span: 3;grid-area: clear;}

footer {
  font-family: system-ui;
  font-size: 2em;
}

footer i {
  font-family: 'Dancing Script', cursive;
  padding-left: 1em;
}
              
            
!

JS

              
                const OPERS = Object.freeze({
  ADD: '+',
  SUBTRACT:  '−',
  MULTIPLY: '×',
  DIVIDE: '÷',
  EQUALS: '='
})

const Footer = (props) => {
  return (
    <footer>made by<i>~Jennifer</i></footer>
  )
}

const Display = (props) => {
    return (
      <div id={props.id} className={props.classes.join(' ')}>{props.content}</div>
    )
}

const Button = (props) => {
    return (
      <div 
        value={props.value}
        data-content={props.content}
        onClick={props.handleFunction}
        id={props.id}
        className={props.classes.join(' ')}>
          {props.content}
      </div>
    );
}



class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      input: '0',
      formula: '0',
      lastOperator: null,
      currentNumHasDecimal: false
    }
    this.handleClear = this.handleClear.bind(this);
    this.handleNumber = this.handleNumber.bind(this);
    this.handleOperator = this.handleOperator.bind(this);
    this.handleEquals = this.handleEquals.bind(this);
    this.resetCalc = this.resetCalc.bind(this);
    this.handleDecimal = this.handleDecimal.bind(this);
  }
  
  handleClear() {
    this.resetCalc();
  }
  
  resetCalc() {
    this.setState(() => ({
      input: '0',
      formula: '0',
      lastOperator: null,
      currentNumHasDecimal: false,
    }))
  }
  
  
  handleNumber(e) {
    if(this.state.lastOperator == OPERS.EQUALS) {
      this.resetCalc();
    }
    const currentNumber = e.target.dataset.content;
    this.setState(state => ({
      input: state.input === '0' ? currentNumber : state.input + currentNumber,
      lastOperator: currentNumber,
    }))
  }
  
  handleOperator(e) {
    if(this.state.lastOperator==OPERS.EQUALS) {
      // change to put formula back in and reset the rest - DONE
      this.setState(state => ({
        formula: state.input,
        input: 0,
        currentNumHasDecimal: false,
      }))
    }

    const currentOp = e.target.dataset.content;
    
    if(this.state.lastOperator===currentOp) return;
    
    const TEST_REGEX = new RegExp('[\\' + OPERS.ADD + '\\' + OPERS.MULTIPLY + '\\' + OPERS.DIVIDE +']$')
    const endsInMinus = new RegExp('[\\' + OPERS.ADD + '\\' + OPERS.MULTIPLY + '\\' + OPERS.DIVIDE +']{1}' + OPERS.SUBTRACT + '$')
    const endsWithAnyOneOperator = new RegExp('[\\' + OPERS.ADD + '\\' + OPERS.SUBTRACT + '\\' + OPERS.MULTIPLY + '\\' + OPERS.DIVIDE +']$')

    // if we end in an operator other than minus, add a minus to the end
    if(TEST_REGEX.test(this.state.lastOperator) && currentOp === OPERS.SUBTRACT) {
      this.setState((state) => ({
        formula: state.formula + currentOp,
        lastOperator: currentOp,
      }))
      return; // don't want to add the number in this case
    }
    // if we have 2 operators ending in a minus then get rid of those and swap it for this one
    else if(endsInMinus.test(this.state.formula)) {
       this.setState(state => ({
         formula: state.formula.slice(0, -2) + currentOp,
         lastOperator: currentOp,
       }))
       return;
     }
    // now just swap whatever the last operator was
    else if (endsWithAnyOneOperator.test(this.state.formula) && endsWithAnyOneOperator.test(this.state.lastOperator)) {
      this.setState(state => ({
        formula: state.formula.slice(0, -1) + currentOp,
        lastOperator: currentOp,
      }))
      return;
    }
    
    
    // add input numbers to formula first
    this.setState((state,props) => {
      let currFormula = state.formula;
      if(currFormula == 0) currFormula = '';
      
      return {
        formula: currFormula + state.input + currentOp,
        input: '0',
        lastOperator: currentOp,
        currentNumHasDecimal: false,
      }
    })
  }

  handleDecimal() {
    if(!this.state.currentNumHasDecimal) {
      this.setState(state => ({
        input: state.input + '.',
        currentNumHasDecimal: true,
        lastOperator: '.',
      }))
    }
  }
  
  handleEquals(e) {
    // if the last operation wasn't equals add the input field to formula first
    if(this.state.lastOperator !== OPERS.EQUALS) {
      let currFormula = this.state.formula;
      if(currFormula == 0) currFormula = '';
      const newFormula = currFormula + this.state.input;
      
      this.setState((state,props) => {
        return {
          formula: newFormula,
          input: '0',
          lastOperator: OPERS.EQUALS,
        }
      })

      // (almost) ready to calc
      const expression = newFormula.replaceAll(OPERS.ADD, '+')
        .replaceAll(OPERS.SUBTRACT, '-')
        .replaceAll(OPERS.MULTIPLY, '*')
        .replaceAll(OPERS.DIVIDE, '/');

      let answer = Function('"use strict";return (' + expression + ');')();

      this.setState(() => ({
        input: answer,
      }))
    }
  }
  
  
  render() {
    return (
      <>
      <div className="calculator-body">
        <div className="display-panel">
          <Display id="formula" classes={['screen', 'output']} content={this.state.formula}/>
          <Display id="display" classes={['screen', 'input']} content={this.state.input}/>
        </div>
        <div className="button-panel">
        <Button value="AC" id="clear" content="C" handleFunction={this.handleClear} classes={['button', 'clear']}/>
        <Button id="divide" content={OPERS.DIVIDE} handleFunction={this.handleOperator} classes={['button', 'operator']} />
        <Button id="multiply" content={OPERS.MULTIPLY} handleFunction={this.handleOperator} classes={['button', 'operator']} />
        <Button id="seven" content="7" handleFunction={this.handleNumber} classes={['button', 'number']} />
        <Button id="eight" content="8" handleFunction={this.handleNumber} classes={['button', 'number']} />
        <Button id="nine" content="9" handleFunction={this.handleNumber} classes={['button', 'number']} />
        <Button id="subtract" content={OPERS.SUBTRACT} handleFunction={this.handleOperator} classes={['button', 'operator']} />
        <Button id="four" content="4" handleFunction={this.handleNumber} classes={['button', 'number']} />
        <Button id="five" content="5" handleFunction={this.handleNumber} classes={['button', 'number']} />
        <Button id="six" content="6" handleFunction={this.handleNumber} classes={['button', 'number']} />
        <Button id="add" content={OPERS.ADD} handleFunction={this.handleOperator} classes={['button', 'operator']} />
        <Button id="one" content="1" handleFunction={this.handleNumber} classes={['button', 'number']} />
        <Button id="two" content="2" handleFunction={this.handleNumber} classes={['button', 'number']} />
        <Button id="three" content="3" handleFunction={this.handleNumber} classes={['button', 'number']} />
        <Button id="equals" content={OPERS.EQUALS} handleFunction={this.handleEquals} classes={['button', 'equals']} />
        <Button id="zero" content="0" handleFunction={this.handleNumber} classes={['button', 'number']} />
        <Button id="decimal" content="." handleFunction={this.handleDecimal} classes={['button', 'number']} />
      </div>
      </div>
        <Footer />
        </>
    )
  }
}

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

Console