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

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

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>JavaScript Calculator</title>
  <!--  Bootstrap CSS  -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
  
<!--  Google Fonts  -->
  <link href="https://fonts.googleapis.com/css?family=VT323" rel="stylesheet">

  <!--  Bootstrap JavaScript  -->
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

</head>

<body class="body h-100 w-100 mx-auto">
  <div class="container-fluid h-100 w-100 mx-auto">
    <h1>JavaScript Calculator</h1>
    <div id="calculator"><!-- calculator ---></div>
    <footer class="footer">
      <small>Built by <a href="https://www.charonworks.com" target="_blank">Charonworks</a></small>
    </footer>
  </div>
</body>

</html>
              
            
!

CSS

              
                body {
    --main-white: #DFF3E4;
    --pale-blue: #7180B9;
}

.body {
  background-color: var(--pale-blue);
  color: #171738;
  font-family: 'VT323', monospace;
}

.container-fluid {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px 10px;
  text-align: center;
}

h1 {
  padding: 20px;
}

.grid-container {
  display: grid;
  width: 280px;
}

.grid-item {
  border: 1px solid var(--pale-blue);
  color: var(--main-white);
  border-radius: 3px;
}

.item-display {
  background-color: #171738;
  color: var(--main-white);
  font-size: 2.5rem;
  font-weight: 100;
  grid-column: 1 / span 4;
  grid-row: 1;
  height: 50px;
  text-align: right;
  overflow: hidden;
  padding: 0 8px;
}

.item-ac {
  grid-column: 1 / span 2;
  grid-row: 2;
}

.item-divide {
  grid-column: 3;
  grid-row: 2;
}

.item-multiply {
  grid-column: 4;
  grid-row: 2;
}

.item-subtract {
  grid-column: 4;
  grid-row: 3;
}

.item-add {
  grid-column: 4;
  grid-row: 4;
}

.item-zero {
  grid-column: 1 / span 2;
  grid-row: 6;
}

.item-dot {
  grid-column: 3;
  grid-row: 6;
}

.item-equals {
  grid-column: 4;
  grid-row: 5 / span 2;
}

.key {
  background-color: #2E1760;
  font-size: 2rem;
  font-weight: 100;
  padding: 15px 25px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
}

.key:hover {
  background-color: #4CAF50;
}

.numKey {
  background-color: #3423A6;
  color: var(--main-white);
}
              
            
!

JS

              
                var nums = {
  0: 'zero',
  1: 'one',
  2: 'two',
  3: 'three',
  4: 'four',
  5: 'five',
  6: 'six',
  7: 'seven',
  8: 'eight',
  9: 'nine'
};

var operators = ['/', '*', '+', '-'];

class Calculator extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      input: '0',
      calculations: []
    };
    this.handleNumber = this.handleNumber.bind(this);
    this.handleOperator = this.handleOperator.bind(this);
    this.clear = this.clear.bind(this);
    this.dot = this.dot.bind(this);
    this.zero = this.zero.bind(this);
    this.calculate = this.calculate.bind(this);
    this.equals = this.equals.bind(this);
  };
  clear(){
    this.setState({
      input: '0',
      calculations: []
    });
  }
  dot(){
    if (this.state.input == '') {
      this.setState({
        input: "0."
      });
    } else if (this.state.input.indexOf(".") == -1) {
      this.setState({
        input: this.state.input + "."
      });
    } else {
      this.setState({
        input: this.state.input
      })
    };
  }
  zero(){
    if (this.state.input != '0') {
      this.setState({
        input: this.state.input + '0'
      });
    };
  }
  handleNumber(event){
    if (this.state.input == '0') {
      this.setState({
        input: event.target.innerText
      });
    } else {
      this.setState({
        input: this.state.input.toString() + event.target.innerText
      });
    }
  }
  handleOperator(event){
    let currentInput = this.state.input;
    let newCalculations = [...this.state.calculations];
    if (currentInput == '') {
      newCalculations.splice(this.state.calculations.length-1, 1,event.target.innerText);
      this.setState({
        input: '',
        calculations: newCalculations
      });
      console.log(this.state.calculations);
    } else {
      this.setState({
        input: '',
        calculations: newCalculations.concat(currentInput ,event.target.innerText)
      });
    }
  }
  calculate(str){
    console.log(str);
    if (str != undefined) {
      var f = new Function('return ' + str + ';');
      return f();
    } else {
      return '0'; 
    }
  }
  equals(event) {
    let currentInput= this.state.input;
    let currentCalculations = this.state.calculations;
    this.setState({
      input: this.calculate(currentCalculations.concat(currentInput).join('')),
      calculations: []
    });
  }
  render() {
    var numKeys = [];
    for (let i=0; i<=9; i++) {
      numKeys.push(
        <RenderNumKey keyName={nums[i]} keyNum={i} onClick={this.handleNumber} />
      )
    }
    return(
      <div className="grid-container">

          <div id="display" className="grid-item item-display">{this.state.calculations}{this.state.input}</div>

        

          <RenderKey keyName="clear" keyValue="AC" className="item-ac" onClick={this.clear} />
          <RenderKey keyName="divide" keyValue="/" className="item-divide" onClick={this.handleOperator} />
          <RenderKey keyName="multiply" keyValue="*" className="item-multiply" onClick={this.handleOperator} />
          <RenderKey keyName="add" keyValue="+" className="item-add" onClick={this.handleOperator} />
          <RenderKey keyName="subtract" keyValue="-" className="item-subtract"  onClick={this.handleOperator} />

        

          {numKeys[7]}{numKeys[8]}{numKeys[9]}


          {numKeys[4]}{numKeys[5]}{numKeys[6]}

          {numKeys[1]}{numKeys[2]}{numKeys[3]}

          <RenderNumKey keyName={nums[0]} keyNum={0} className="item-zero" onClick={this.zero} />
          <RenderKey keyName="decimal" keyValue="." className="item-dot" onClick={this.dot} />
          <RenderKey keyName="equals" keyValue="=" className="item-equals" onClick={this.equals} />

      </div>
    );
  }
};

class RenderKey extends React.Component {
  constructor(props) {
    super(props);
  }
  render(){
    return(
    <button id={this.props.keyName} className={'grid-item key ' + this.props.className} onClick={this.props.onClick}>{this.props.keyValue}</button>
    );
  }
}

class RenderNumKey extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    return (
      <button id={this.props.keyName} className={'numKey key grid-item ' + this.props.className} onClick={this.props.onClick}>{this.props.keyNum}</button>
    );
  }
}

ReactDOM.render(<Calculator />, document.getElementById('calculator'));

              
            
!
999px

Console