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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

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

CSS

            
              body {
  display: flex;
  justify-content: center;
}

h2#title {
  margin: 2px;
  text-align: center;
}

.calculator {
  padding: 10px;
  border: 2px solid black;
  border-radius: 10px;
  background-color: #dfd8d0;
  height: 350px; /* Not sure why I can't resize */
}

.display-wrapper {
  background-color: #c3c2ab;
  border-radius: 10px;
  border: 2px solid black;
  text-align: right;
  padding-right: 5px;
  min-height: 40px;
  max-width: 230px;
  position: relative;
}

.display-wrapper p {
  margin: 0px;
}
.display-wrapper #display {
  min-height: 20px;
  width: 100%;
  word-wrap: break-word;
  font-family: consolas;
  font-size: 120%;
}
.display-wrapper #currentFormula {
  min-height: 20px;
  width: 100%;
  word-wrap: break-word;
  font-size: 90%;
  font-family: arial;
}

/*********************** BUTTONS ****************/
/* https://gridbyexample.com/examples/example19/ */
.buttons {
  display: grid;
  grid-template-columns: repeat(4, 50px);
  grid-template-rows: repeat(5, 20%);
  grid-gap: 10px;
  margin-top: 10px;
}
.buttons button {
  padding: 5px;
  border-radius: 5px;
  font-size: 110%;
  background-color: black;
  color: white;
}
.buttons button[value="AC"],
.buttons button[value="CE"] {
  background-color: #a72d45;
  /* dark red */
}
.buttons button[value="AC"] {
  grid-column: 1/3;
}
.buttons #equals {
  grid-column: 4/5;
  grid-row: 4/6;
}
.buttons #zero {
  grid-row: 5/6;
  grid-column: 1/3;
}
            
          
!

JS

            
              const isOperator = /[*\/+-]/;
const hasDecimal = /[.]/;
const endsWithOperator = /[*\/+-]$/;

class App extends React.Component {
  /*
   * STATE VARIABLES
   * CurValue is the current numeric or operator token
   * Formula is the full operation before `eval` is called
   * evaluated is a true/false flag to let other methods handle reset logic
   * Unused variables: prevValue, curSign, lastClicked
   */
  state = {
    curValue: 0,
    formula: "",
    evaluated: false
  };
  componentDidMount() {
    console.log("I was triggered during componentDidMount");
  }
  componentDidUpdate() {
    console.log("I updated");
  }
  handleClearAll = () => {
    this.setState({
      curValue: 0,
      formula: "",
      evaluated: false
    });
  };
  handleOperators = e => {
    if (this.state.evaluated) {
      this.setState({
        formula: this.state.curValue + e.target.value,
        curValue: e.target.value,
        evaluated: false
      });
    } else {
      // Disallow first value as operator
      if (!(this.state.curValue === 0)) {
        // Forbid sequential operators
        if (endsWithOperator.test(this.state.formula)) {
          this.setState({
            // Allow operator change on current token
            curValue: e.target.value,
            formula: this.state.formula.slice(0, -1) + e.target.value
          });
        } else {
          this.setState({
            curValue: e.target.value,
            formula: this.state.formula + e.target.value
          });
        }
      }
    }
  };
  handleNumbers = e => {
    // Reset if "=" was last command
    if (this.state.evaluated) {
      this.setState({
        curValue: e.target.value,
        formula: e.target.value == 0 ? "" : e.target.value,
        evaluated: false
      });
    } else {
      if (this.state.curValue === 0 && e.target.value == 0) {
        // do nothing
      } else if (
        this.state.curValue === 0 ||
        isOperator.test(this.state.curValue)
      ) {
        // Remove 0 or operator replace with [1-9] number
        this.setState({
          curValue: e.target.value,
          formula: this.state.formula + e.target.value
        });
      } else {
        // Capture a sequence of numbers
        this.setState({
          curValue: this.state.curValue + e.target.value,
          formula: this.state.formula + e.target.value
        });
      }
    }
  };
  handleEvaluate = () => {
    let tempFormula = this.state.formula;
    // Remove last char if operator
    if (endsWithOperator.test(tempFormula)) {
      tempFormula = tempFormula.slice(0, -1);
    }
    // order of operations calculate
    let evaluate = Math.round(10000000000 * eval(tempFormula)) / 10000000000;
    this.setState({
      curValue: evaluate,
      formula: tempFormula + "=" + evaluate,
      evaluated: true
    });
  };
  handleDecimal = () => {
    // If initialized, or just evaluated, the first value must be "0."
    if (this.state.curValue === 0 || this.state.evaluated) {
      this.setState({
        curValue: "0.",
        formula: "0.",
        evaluated: false
      });
      // If operator previously used, append result
    } else if (isOperator.test(this.state.curValue)) {
      this.setState({
        curValue: "0.",
        formula: this.state.formula + "0."
      });
      // otherwise just add a period
    } else if (!hasDecimal.test(this.state.curValue)) {
      this.setState({
        curValue: this.state.curValue + ".",
        formula: this.state.formula + "."
      });
    }
  };
  render() {
    return (
      <div className="container">
        <div className="calculator">
          <Header />
          <div className="display-wrapper">
            <Formula formula={this.state.formula} />
            <Output curValue={this.state.curValue} />
          </div>
          <Buttons
            clearAll={this.handleClearAll}
            clearEntry={this.handleClearEntry}
            operators={this.handleOperators}
            numbers={this.handleNumbers}
            evaluate={this.handleEvaluate}
            decimal={this.handleDecimal}
          />
        </div>
      </div>
    );
  }
}

class Buttons extends React.Component {
  render(){
    return(
     <div className="buttons">
    <button id="clear" onClick={this.props.clearAll} value="AC">
      AC
    </button>
    {/*<button id="clear-entry" onClick={props.clearEntry} value="CE">
      CE
    </button>*/}
    <button id="divide" onClick={this.props.operators} value="/">
      ÷
    </button>
    <button id="multiply" onClick={this.props.operators} value="*">
      x
    </button>
    <button id="seven" onClick={this.props.numbers} value="7">
      7
    </button>
    <button id="eight" value="8" onClick={this.props.numbers}>
      8
    </button>
    <button id="nine" value="9" onClick={this.props.numbers}>
      9
    </button>
    <button id="subtract" onClick={this.props.operators} value="-">
      -
    </button>
    <button id="four" value="4" onClick={this.props.numbers}>
      4
    </button>
    <button id="five" value="5" onClick={this.props.numbers}>
      5
    </button>
    <button id="six" value="6" onClick={this.props.numbers}>
      6
    </button>
    <button id="add" onClick={this.props.operators} value="+">
      +
    </button>
    <button id="one" value="1" onClick={this.props.numbers}>
      1
    </button>
    <button id="two" value="2" onClick={this.props.numbers}>
      2
    </button>
    <button id="three" value="3" onClick={this.props.numbers}>
      3
    </button>
    <button id="equals" value="=" onClick={this.props.evaluate}>
      =
    </button>
    <button id="zero" value="0" onClick={this.props.numbers}>
      0
    </button>
    <button id="decimal" value="." onClick={this.props.decimal}>
      .
    </button>
  </div>
      )
  }
}
 

const Formula = props => <p id="currentFormula">{props.formula}</p>;

const Header = () => <h2 id="title">Electronic Calculator</h2>;

const Output = props => <p id="display">{props.curValue}</p>;

ReactDOM.render(<App />, document.getElementById("root"));


            
          
!
999px

Console