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 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

              
                body {
  padding-top: 80px;
  font-family: 'Roboto Mono', monospace;
  background-color: #DBEAF8;
}

.calculator {
  border: 2px solid black;
  border-radius: 2%;
  margin: 0 auto;
  width: 289px;
  padding: 7px 7px 7px 7px;
  background-color: black;
}

button {
  border: 1px grey solid;
  background-color: gray;
  margin: 1px 1px 1px 1px;
  color: white;
}

.num-buttons {
  background-color: #333333;
  width: 70px;
  height: 70px;
  font-size: 1.5rem;
}

.num-buttons-wrapper {
  width: 218px;
}

.op-buttons {
  width: 70px;
  height: 70px;
  font-size: 1.5rem;
  background-color: #F79500;
}

.op-buttons-wrapper {
  float: right;
  display: flex;
  flex-direction: column;
  margin-top: -72px;
}

.equals-decimal-wrapper {
  float: right;
  margin-top: -72px;
}

#decimal {
  width: 70px;
  height: 70px;
  font-size: 1.5rem;
}

#equals {
  width: 70px;
  height: 70px;
  font-size: 1.5rem;
}

#zero {
  width: 142px;
}

#clear {
  width: 213px;
  height: 70px;
  font-size: 1.5rem;
}

#display {
  text-align: right;
  font-size: 2rem;
  padding-right: 2px;
  color: white;
  overflow: hidden;
}

#display-trail {
  text-align: right;
  font-size: 1rem;
  padding-right: 2px;
  padding-top: 5px;
  color: white;
  overflow: hidden;
}

p {
  font-size: 0.8rem;
  color: dark-grey;
  text-align: center;
}
              
            
!

JS

              
                const CAL_NUMBERS = [{
      id: 'one',
      number: 1
    }, {
      id: 'two',
      number: 2
    }, {
      id: 'three',
      number: 3
    }, {
      id: 'four',
      number: 4
    }, {
      id: 'five',
      number: 5
    }, {
      id: 'six',
      number: 6
    }, {
      id: 'seven',
      number: 7
    }, {
      id: 'eight',
      number: 8
    }, {
      id: 'nine',
      number: 9
    }, {
      id: 'zero',
      number: 0
    }];

    const ARITHMETIC_OPERATOR = [{
      id: 'add',
      operator: '+'
    }, {
      id: 'subtract',
      operator: '-'
    }, {
      id: 'multiply',
      operator: 'x'
    }, {
      id: 'divide',
      operator: '/'
    }];

    let numbers = [],
        operator = '',
        display = [],
        total = [],
        joinNums = []


     class App extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
          display: 0,
          input: 0
        };
      }

      numberClickedHandler = (event) => {
        // executes the if statement when an operator is added
        if (operator !== '') {
          total.push(joinNums);
          numbers = [];
          display.push(operator);
          this.setState({
            display: display
          });
          total.push(operator);
          operator = '';
        }

        // to add a deminal point after zero
        if (display === "0.") {
          for (let i = 0; i < CAL_NUMBERS.length; i++) {
            if (event.target.id === CAL_NUMBERS[i].id) {
              display.push(CAL_NUMBERS[i].number);
              numbers.push(CAL_NUMBERS[i].number);
            }
          }
          this.setState({
            display: display
          });
          joinNums = numbers.join('');
          this.setState({
            input: joinNums
          });
          numbers = [];
          // to prevent input of multiple zeros
        } else if (event.target.id === 'zero' && numbers.length === 0) {
            display.push('0');
            numbers.push('0');
            joinNums = numbers.join('');
            numbers = [];
            this.setState({
              display: display,
              input: 0
            });
          // to add whole integers
        } else {
          for (let i = 0; i < CAL_NUMBERS.length; i++) {
            if (event.target.id === CAL_NUMBERS[i].id) {
              display.push(CAL_NUMBERS[i].number);
              numbers.push(CAL_NUMBERS[i].number);
            }
          }
          this.setState({
            display: display
          });
          joinNums = numbers.join('');
          this.setState({
            input: joinNums
          });
        }
      }

      // add operator +-*/
      opClickedHandler = (event) => {
        if (event.target.id === 'add') {
          operator = '+';
        }
        if (event.target.id === 'subtract') {
          operator = '-';
        }
        if (event.target.id === 'multiply') {
          operator = '*';
        }
        if (event.target.id === 'divide') {
          operator = '/';
        }
      }

      // calculates total
      equalsClickedHandler = () => {
        // if zero is added after an operator
        if (this.state.input === 0) {
          total.push('0');
          display.push('=');
          let convertTotalToString = total.join('').toString();
          // use eval() method to total calculation
          let newTotal = eval(convertTotalToString);
          newTotal = parseFloat(newTotal.toFixed(4));
          display.push(newTotal);
          joinNums = [newTotal];
          this.setState({
            display: display,
            input: joinNums
          });
          total = []; //Reset total
          // any other scenario
        } else {
          numbers = [];
          display.push('=');
          this.setState({
            display: display
          });
          total.push(joinNums);
          // convert total array to string
          let convertTotalToString = total.join('').toString();
          // use eval() method to total calculation
          let newTotal = eval(convertTotalToString);
          newTotal = parseFloat(newTotal.toFixed(4));
          display.push(newTotal);
          joinNums = [newTotal];
          this.setState({
            display: display,
            input: joinNums
          });
          total = []; //Reset total
        }
      }

      decimalClickedHandlers = () => {
        // execute if the number array is empty
        if (numbers.length === 0 && operator === '')  {
          display.push('.');
          numbers.push('0.');
          this.setState({
            display: display,
            input: numbers
          });
          // click on the decimal button after an operator
        } else if (operator !== '') {
          joinNums = numbers.join('');
          total.push(joinNums);
          total.push(operator);
          numbers = [];
          display.push(operator);
          display.push('0.');
          numbers.push('0.')
          this.setState({
            display: display,
            input: numbers,
          });
          operator = '';
          // add '.' to number array if numbers array doesn't already have '.' & if it has a integer >=1
        } else if (numbers !== '0.' && numbers >= 1 ) {
          display.push('.');
          numbers.push('.');
          // number array has >= 2 items & check last item in the numbers array = number & doesn't already contain decimal point
        } else if (numbers.length >= 2 && typeof numbers[numbers.length-1] === "number"
        && numbers.includes('.') === false) {
          display.push('.');
          numbers.push('.');
        }
      }

      resetClickedHandler = () => {
        this.setState({
          display: 0,
          input: 0
        });
        numbers = [];
        operator = '';
        display = [];
        total = [];
        joinNums = [];
      }
      
      render() {
        return (
          <div>
            <div className="calculator">
              <div id="display-trail">
                {this.state.display}
              </div>

              <div id="display">
                {this.state.input}
                </div>

              <div>
                <button
                  id="clear" onClick={this.resetClickedHandler}>A/C
                </button>
              </div>

              <div className="op-buttons-wrapper">
              {ARITHMETIC_OPERATOR.map((opMapped, index) => {
                return (
                  <Operator
                    PadOp={opMapped.operator}
                    id={opMapped.id}
                    key={index}
                    opclicked={this.opClickedHandler}
                    />
                  )
                })
              }
              </div>

              <div className="num-buttons-wrapper">
              {CAL_NUMBERS.map((numMapped, index) => {
                return (
                  <NumPad
                    PadNum={numMapped.number}
                    id={numMapped.id}
                    key={index}
                    numclick={this.numberClickedHandler}
                    />
                  )
                })
              }
              </div>

              <div className="equals-decimal-wrapper">
                <button onClick={this.decimalClickedHandlers} id="decimal">.
                </button>
                <button
                  id="equals" onClick={this.equalsClickedHandler}>=
                </button>
              </div>

            </div>
            <p class="text-center">&copy; 2020 Phoebe Voong-Fadel | The Coding Hamster. All rights reserved.</p>
          </div>
        );
      }
    }

    const NumPad = (props) => {
      return (
        <button
          className="num-buttons"
          id={props.id}
          onClick={props.numclick}>
          {props.PadNum}
        </button>
      )
    }

    const Operator = (props) => {
      return (
        <button
          className="op-buttons"
          id={props.id}
          onClick={props.opclicked}>
          {props.PadOp}
        </button>
      )
    }

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

Console