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>-->
<header>
  <link href="https://fonts.googleapis.com/css2?family=Chelsea+Market&display=swap" rel="stylesheet">
         <link href="https://fonts.googleapis.com/css?family=Orbitron&display=swap" rel="stylesheet">
           </header>
<h1><span id="react">React</span> Calculator</h1>
<div id="root"></div>
              
            
!

CSS

              
                body {
  margin: 0px;
  background: #444;
}

h1{
  //position: absolute;
  width: 100vw;
  text-align: center;
  font-family: 'Chelsea Market', cursive;
  color: #fff;
  background: #444;
  //margin: 0;
  margin-bottom: -40px;
  //margin-top: 40;
}

#react{
  color: #79D8F8;
}

#container {
  display: grid;
  width: 100vw;
  height: 100vh;
  justify-content: center;
  align-items: center;
}

#calculator-container {
  display: grid;
  width: 50vw;
  height: 30.902vw;
  min-width: 280px;
  min-height: 210px;

  grid-template-columns: 20% 20% 20% 20% 20%;
  grid-template-rows: 20% 20% 20% 20% 20%;
  grid-column-gap: 0px;
  grid-row-gap: 0px;
  background: #FFF;
  border-top: solid #FFF 5px;
  border-left: solid #FFF 5px;
  border-right: solid #FFF 5px;
  border-radius: 5px;
}

#display {
  display: grid;
  grid-column-start: 1;
  grid-column-end: 6;
  grid-row-start: 1;
  grid-row-end: 2;
  background: #bfb;
  justify-content: right;
  align-items: center;
  padding: 10px;
  overflow: hidden;
  font-family: 'Orbitron';
  font-size: 45px;
}

.button {
  display: grid;
  justify-content: center;
  align-items: center;
  background: #FFF;
  
}

.button button {
  background: #68ABF4;
  color: #FFF;
  border: solid white;
  border-width: 1px;
  border-radius: 5px;
  width: 9vw;
  height: 4vw;
  min-width: 45px;
  font-family: 'Orbitron';
  font-size: 18px;
}

.button-operator {
  display: grid;
  justify-content: center;
  align-items: center;
  background: #FFF;
}

.button-operator button {
  background: #FBA021;
  color: white;
  border: solid white;
  border-width: 1px;
  border-radius: 5px;
  width: 9vw;
  height: 4vw;
  min-width: 45px;
  font-family: 'Orbitron';
  font-size: 25px;
}

#zero-container {
  grid-column-start: 1;
  grid-column-end: 2;
  grid-row-start: 5;
  grid-row-end: 6;
}

#one-container {
  grid-column-start: 1;
  grid-column-end: 2;
  grid-row-start: 4;
  grid-row-end: 5;
}

#two-container {
  grid-column-start: 2;
  grid-column-end: 3;
  grid-row-start: 4;
  grid-row-end: 5;
}

#three-container {
  grid-column-start: 3;
  grid-column-end: 4;
  grid-row-start: 4;
  grid-row-end: 5;
}

#four-container {
  grid-column-start: 1;
  grid-column-end: 2;
  grid-row-start: 3;
  grid-row-end: 4;
}

#five-container {
  grid-column-start: 2;
  grid-column-end: 3;
  grid-row-start: 3;
  grid-row-end: 4;
}

#six-container {
  grid-column-start: 3;
  grid-column-end: 4;
  grid-row-start: 3;
  grid-row-end: 4;
}

#seven-container {
  grid-column-start: 1;
  grid-column-end: 2;
  grid-row-start: 2;
  grid-row-end: 3;
}

#eight-container {
  grid-column-start: 2;
  grid-column-end: 3;
  grid-row-start: 2;
  grid-row-end: 3;
}

#nine-container {
  grid-column-start: 3;
  grid-column-end: 4;
  grid-row-start: 2;
  grid-row-end: 3;
}

#decimal-container {
  grid-column-start: 2;
  grid-column-end: 3;
  grid-row-start: 5;
  grid-row-end: 6;
}

#clear-container {
  grid-column-start: 3;
  grid-column-end: 4;
  grid-row-start: 5;
  grid-row-end: 6;
}

#equals-container {
  display: grid;
  justify-content: center;
  align-items: center;
  grid-column-start: 5;
  grid-column-end: 6;
  grid-row-start: 2;
  grid-row-end: 6;
  background: #FFF;
}

#equals {
  background: #AC81AC;
  height: 91%;
  width: 9vw;
  min-width: 45px;
  min-height: 145px;
  font-family: 'Orbitron';
  font-size: 34px;
}

@media only screen and (max-width: 1000px) {
  button {
  font-size: 15px;
}
#display {
  font-size: 30px;
}
  
}

@media only screen and (max-width: 800px) {
.button-operator button {
  font-size: 15px;
}
  
.button button {
  font-size: 15px;
}
  
#equals {
  font-size: 20px;
  height: 89%;
}
  
#display {
  font-size: 25px;
}
  
}

@media only screen and (max-width: 600px) {
.button-operator button {
  font-size: 12px;
}
  
.button button {
  font-size: 10px;
}
  
#equals {
  font-size: 15px;
  height: 89%;
}
  
#display {
  font-size: 22px;
}
  
}
              
            
!

JS

              
                class App extends React.Component {
  constructor() {
    super();
    //this.keyFunction = this.keyFunction.bind(this);
    this.state = {
      display: 0,
      theCount: 0,
      theResult: 0,
      decimal: 0
    };
  }
  
  updateDisplay(event) {
    if (this.state.display != "0") {
      let newNum = this.state.display + event.target.value;
      this.setState({ display: newNum });
      this.setState({ theCount: newNum });
    } else {
      let newNum = event.target.value;
      this.setState({ display: newNum });
      this.setState({ theCount: newNum });
    }
  }

  updateDisplayDecimal(event) {
    if (this.state.decimal === 0) {
      let newNumDecimal = this.state.display + event.target.value;
      this.setState({ decimal: 1 });
      this.setState({ display: newNumDecimal });
    }
  }

  clearState(event) {
    this.setState({ display: 0 });
    this.setState({ decimal: 0 });
  }

  addition(event) {
    let displayCopy = 0;
    let lastChar = 0;
    //If display isn't blank
    if (this.state.display != 0) {
      //Make a copy of the display (just the number)
      //Convert it to a string first though so that charAt can work properly
      displayCopy = this.state.display.toString(2);
      //Get the last character of the display
      lastChar = displayCopy.charAt(displayCopy.length - 1);
      
      //If the last character is a number then add a plus sign to the display
      if (lastChar.match(/[0-9]/g)) {
        this.setState({ display: this.state.display + "+" });
        this.setState({ decimal: 0 });
      }
   
      /*While the last character is anything else, keep removing characters until a number is reached... then add a plus sign to the display*/
      while(!lastChar.match(/[0-9]/g)){
        displayCopy = displayCopy.substring(0, displayCopy.length - 1);
        lastChar = displayCopy.charAt(displayCopy.length - 1);
        this.setState({ display: displayCopy + "+"});
      }
    }
  }

  subtraction(event) {
    let displayCopy = 0;
    let lastChar = 0;
    //If display isn't blank
    if (this.state.display != 0) {
      //Make a copy of the display (just the number)
      //Convert it to a string first though so that charAt can work properly
      displayCopy = this.state.display.toString(2);
      //Get the last character of the display
      lastChar = displayCopy.charAt(displayCopy.length - 1);
      
      //If the last character is a number then add a minus sign to the display
      if (lastChar.match(/[0-9]/g)) {
        this.setState({ display: this.state.display + "-" });
        this.setState({ decimal: 0 });
      } else {
      //If the last character is not a number then remove it and add a minus sign to the display
        this.setState({ display: this.state.display + "-" });
        this.setState({ decimal: 0 });
      }
    }
  }

  multiplication(event) {
    let displayCopy = 0;
    let lastChar = 0;
    //If display isn't blank
    if (this.state.display != 0) {
      //Make a copy of the display (just the number)
      //Convert it to a string first though so that charAt can work properly
      displayCopy = this.state.display.toString(2);
      //Get the last character of the display
      lastChar = displayCopy.charAt(displayCopy.length - 1);
      
      //If the last character is a number then add a times sign to the display
      if (lastChar.match(/[0-9-]/g)) {
        this.setState({ display: this.state.display + "*" });
        this.setState({ decimal: 0 });
      } else {
        //If the last character is not a number then remove it and add a times sign to the display
        displayCopy = displayCopy.replace(/.$/, "*");
        this.setState({ display: displayCopy });
        this.setState({ decimal: 0 });
      }
    }
  }

  division(event) {
    let displayCopy = 0;
    let lastChar = 0;
    //If display isn't blank
    if (this.state.display != 0) {
      //Make a copy of the display (just the number)
      //Convert it to a string first though so that charAt can work properly
      displayCopy = this.state.display.toString(2);
      //Get the last character of the display
      lastChar = displayCopy.charAt(displayCopy.length - 1);
      
      //If the last character is a number then add a division sign to the display
      if (lastChar.match(/[0-9-]/g)) {
        this.setState({ display: this.state.display + "/" });
        this.setState({ decimal: 0 });
      } else {
        //If the last character is not a number then remove it and add a division sign to the display
        displayCopy = displayCopy.replace(/.$/, "/");
        this.setState({ display: displayCopy });
        this.setState({ decimal: 0 });
      }
    }
  }

  equals(event) {
    let result = eval(this.state.display);
    this.setState({ display: result });
  }

  render() {
    return (      
      <div id="container">
        <div id="calculator-container">
          <div className="button" id="equals-container">
            <button onClick={this.equals.bind(this)} id="equals">
              =
            </button>
          </div>

          <div className="button" id="zero-container">
            <button onClick={this.updateDisplay.bind(this)} value="0" id="zero">
              0
            </button>
          </div>

          <div className="button" id="one-container">
            <button onClick={this.updateDisplay.bind(this)} value="1" id="one">
              1
            </button>
          </div>

          <div className="button" id="two-container">
            <button onClick={this.updateDisplay.bind(this)} value="2" id="two">
              2
            </button>
          </div>

          <div className="button" id="three-container">
            <button
              onClick={this.updateDisplay.bind(this)}
              value="3"
              id="three"
            >
              3
            </button>
          </div>

          <div className="button" id="four-container">
            <button onClick={this.updateDisplay.bind(this)} value="4" id="four">
              4
            </button>
          </div>

          <div className="button" id="five-container">
            <button onClick={this.updateDisplay.bind(this)} value="5" id="five">
              5
            </button>
          </div>

          <div className="button" id="six-container">
            <button onClick={this.updateDisplay.bind(this)} value="6" id="six">
              6
            </button>
          </div>

          <div className="button" id="seven-container">
            <button
              onClick={this.updateDisplay.bind(this)}
              value="7"
              id="seven"
            >
              7
            </button>
          </div>

          <div className="button" id="eight-container">
            <button
              onClick={this.updateDisplay.bind(this)}
              value="8"
              id="eight"
            >
              8
            </button>
          </div>

          <div className="button" id="nine" id="nine-container">
            <button onClick={this.updateDisplay.bind(this)} value="9" id="nine">
              9
            </button>
          </div>

          <div className="button-operator" id="add-container">
            <button onClick={this.addition.bind(this)} value="+" id="add">
              +
            </button>
          </div>

          <div className="button-operator" id="subtract-container">
            <button
              onClick={this.subtraction.bind(this)}
              value="-"
              id="subtract"
            >
              -
            </button>
          </div>
          <div className="button-operator" id="multiply-container">
            <button
              onClick={this.multiplication.bind(this)}
              value="x"
              id="multiply"
            >
              ×
            </button>
          </div>
          <div className="button-operator" id="divide-container">
            <button onClick={this.division.bind(this)} value="÷" id="divide">
              ÷
            </button>
          </div>

          <div className="button" id="decimal-container">
            <button
              onClick={this.updateDisplayDecimal.bind(this)}
              value="."
              id="decimal"
            >
              .
            </button>
          </div>

          <div className="button" id="clear-container">
            <button
              onClick={this.clearState.bind(this)}
              value={this.state.display}
              id="clear"
            >
              c
            </button>
          </div>

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

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

              
            
!
999px

Console