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.

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

              
                <!-- <script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script> -->

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>JavaScript Calculator</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Load Bootstrap. -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

 
    <!-- Load React. -->
    <!-- Note: when deploying, replace "development.js" with "production.min.js". -->
    <script src="https://unpkg.com/react@16/umd/react.production.min.js" crossorigin></script>
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js" crossorigin></script>
      <!-- Load our React component. -->
    
  </head>

  <body>
    <div id="content">
      <div id="js-calculator"></div>
      <p>Photo by John Moeses Bauan on Unsplash</p>
    </div>
  </body>
    
</html>
              
            
!

CSS

              
                #js-calculator {
  margin: auto;
  margin-top: 1.3rem;
  margin-bottom: 1rem;
  width: 34rem;
  background: lightgreen;
  border: 2px lightgreen solid;
  border-radius: 30px;
  padding: 1rem;
  justify-content: center;
  text-align: center;
}

#title {
  padding-bottom: 0.3em;
  font-size: 2em;
  font-weight: bold;
  color: blue;
}

#displayComplete {
  display: block;
  margin: auto;
  width: 28rem;
  background: black;
  padding-right: 0.9rem;
  text-align: right;
  color: blue;
  font-family: monospace;
  font-size: 3rem;
  font-weight: bold;
}

#displayFormula {
  font-size: 3rem;
  color: orange;
}

#numPad {
  margin: auto;
  padding: 1em;
  padding-top: 1.5em;
  width: 250px;
}

.btn {
  width: 5.5rem;
  height: 6.7rem;
}

.btn-primary {
  font-size: 4.5rem;
}

#clear {
  width: 100px;
}

#content {
  text-align: center;
}

body {
  background-image: url("https://images.unsplash.com/photo-1528082992860-d520150d6f6c?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjQ5ODZ9&auto=format&fit=crop&w=934&q=80");
  width: 100%;
  height: 100%;
}

#backwards {
  width: 130px;
  text-decoration: none;
}

#backwards a {
  color: white;
  font-family: sans-serif;
}

.btn-primary {
  font-family: monospace;
  font-size: 4.5rem;
}

@media only screen (min-width: 800) {

  #js-calculator {
    width: 60rem;
    padding: 1.5rem;
    margin-top: 2rem;
    margin-bottom: 2rem;
    justify-content: center;
    text-align: center;
  }

  #title {
    padding-bottom: 0.5em;
    font-size: 3em;
  }

  #displayComplete {
    display: block;
    margin: auto;
    width: 55rem;
    background: black;
    padding-right: 1.2rem;
    text-align: right;
    font-size: 4rem;
  }

  #displayFormula {
    font-size: 4rem;
  }

  #numPad {
    padding: 1.5em;
    padding-top: 12em;
    width: 40rem;
  }

  .btn {
    width: 8rem;
    height: 10rem;
  }

  .btn-primary {
    font-size: 7rem;
  }

  #clear {
    width: 16rem;
  }

  .btn-primary {
    font-size: 7rem;
  }
}

              
            
!

JS

              
                const endOperator = /[x+/-]$/,
      multiDivide = /[x/]/,
      plusMinus = /[+-]/,
      maxDigits = 15,
      initialState = {
        displayInput: 0,
        displayStore: '0',
        stateDecimal: false,
        firstValueZero: true,
        firstValue: true,
        stateOperator: false,
        stateEquals: false,
        formula: ''
      };


class CalcPad extends React.Component {
  constructor(props) {
    super(props);
    this.state = initialState;
  }

  handleNumberClick(e)  {
      if (this.state.displayInput=='0') {
        this.setState({
          displayInput: e.target.value
        });
      } else if (this.state.stateOperator===true && this.state.stateEquals===false) {
        this.setState({
          displayStore: this.state.displayStore + this.state.displayInput,
          displayInput: e.target.value,
          stateOperator: false,
          stateDecimal: false
        });
      } else if (this.state.stateOperator===false && this.state.stateEquals===true) {
        this.setState({
          displayStore: this.state.displayStore,
          displayInput: e.target.value,
          stateOperator: false,
          stateDecimal: false,
          stateEquals: false,
          firstValue: false
        });
      } else if (this.state.displayInput.length < maxDigits) {
       this.setState({
          displayInput: this.state.displayInput + e.target.value
        });
      }
  }

  deciClick(e) {
    if (this.state.stateOperator) {
      this.setState({
        displayStore: this.state.displayStore + this.state.displayInput,
        displayInput: '0.',
        stateDecimal: true,
        stateOperator: false
      });
    } else if (this.state.stateDecimal===false) {
      this.setState({
        displayInput: this.state.displayInput + e.target.value,
        stateDecimal: true
      });
    }
  }

  handleOperatorClick(e) {
    if (this.state.displayStore=='0') {
      console.log("Hello You");
        this.state.displayStore='';
    }
    if (this.state.stateOperator===false && this.state.stateEquals===false && this.state.firstValue===true) {
      this.setState({
        displayStore: this.state.displayStore + this.state.displayInput,
        displayInput: e.target.value,
        stateDecimal: false,
        stateOperator: true,
        stateEquals: false
      });
    } else if (this.state.stateOperator===false && this.state.stateEquals===true && this.state.firstValue===false) {
      this.setState({
        displayStore: this.state.displayInput,
        displayInput: e.target.value,
        stateDecimal: false,
        stateOperator: true,
        stateEquals: false,
        firstValue: true
      });
    } else if (this.state.stateOperator===false && this.state.stateEquals===false && this.state.firstValue===false) {
      this.setState({
        displayStore: this.state.displayInput,
        displayInput: e.target.value,
        stateDecimal: false,
        stateOperator: true,
        stateEquals: false,
        firstValue: true
      });
    } else if (multiDivide.test(this.state.displayInput) && plusMinus.test(e.target.value) && this.state.displayInput.length <= 1) {
       this.setState({
         displayInput: this.state.displayInput + e.target.value,
         stateDecimal: false,
         stateOperator: true
       });
    } else {
         this.setState({
           displayInput: e.target.value,
           stateDecimal: false,
           stateOperator: true
       });
    }
  }

  handleEqualsClick() {
    let formula = this.state.displayStore+this.state.displayInput;
    if (endOperator.test(formula)) {
      formula=formula.slice(0, -1);
    }
    formula = formula.replace(/x/g, "*");
    let result = eval(formula);
    this.setState({
      displayStore: result,
      displayInput: result,
      firstValue: false,
      stateDecimal: false,
      stateOperator: false,
      stateEquals: true
    });
  }

  clearClick() {
    this.setState(initialState);
  }

  render() {
    return (
      <div>
        <h1 id="title">JavaScript Calculator</h1>
        <div id="displayComplete">
            <p id="displayFormula">{this.state.displayStore}</p>
            <p id="display">{this.state.displayInput}</p>
        </div>
        <div id="numPad">
          <button id="zero" className="btn btn-primary" onClick={this.handleNumberClick.bind(this)} value="0">0</button>
          <button id="one" className="btn btn-primary" onClick={this.handleNumberClick.bind(this)} value="1">1</button>
          <button id="two" className="btn btn-primary" onClick={this.handleNumberClick.bind(this)} value="2">2</button>
          <button id="three" className="btn btn-primary" onClick={this.handleNumberClick.bind(this)} value="3">3</button>
          <button id="four" className="btn btn-primary" onClick={this.handleNumberClick.bind(this)} value="4">4</button>
          <button id="five" className="btn btn-primary" onClick={this.handleNumberClick.bind(this)} value="5">5</button>
          <button id="six" className="btn btn-primary" onClick={this.handleNumberClick.bind(this)} value="6">6</button>
          <button id="seven" className="btn btn-primary" onClick={this.handleNumberClick.bind(this)} value="7">7</button>
          <button id="eight" className="btn btn-primary" onClick={this.handleNumberClick.bind(this)} value="8">8</button>
          <button id="nine" className="btn btn-primary" onClick={this.handleNumberClick.bind(this)} value="9">9</button>
          <button id="add" className="btn btn-warning" onClick={this.handleOperatorClick.bind(this)} value="+">+</button>
          <button id="subtract" className="btn btn-warning" onClick={this.handleOperatorClick.bind(this)} value="-">-</button>
          <button id="multiply" className="btn btn-warning" onClick={this.handleOperatorClick.bind(this)} value="x">*</button>
          <button id="divide" className="btn btn-warning" onClick={this.handleOperatorClick.bind(this)} value="/">/</button>
          <button id="equals" className="btn btn-warning" onClick={this.handleEqualsClick.bind(this)} value="=">=</button>
          <button id="decimal" className="btn btn-warning" onClick={this.deciClick.bind(this)} value=".">.</button>
          <button id="clear" className="btn btn-warning" onClick={()=>this.clearClick()}>CLEAR</button>
        </div>
      </div>
     );
  }
}

ReactDOM.render(
  <CalcPad />, document.getElementById("js-calculator")
);
              
            
!
999px

Console