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" />
    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, shrink-to-fit=no"
    />
    <meta name="theme-color" content="#000000" />

    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    <link
      href="https://fonts.googleapis.com/css?family=Orbitron"
      rel="stylesheet"
    />

    <link
      href="https://fonts.googleapis.com/css?family=Rajdhani"
      rel="stylesheet"
    />
    <title>React Calculator</title>
 
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
  </body>
</html>

              
            
!

CSS

              
                $bg-color: #282c34;
$color-dark-purple: #1f2029;
$display-color: #b4cca6;
$inner-shadow: inset 0 -2px 4px #cecece, inset 0 1px 15px #000;
$top-left-gr-black: linear-gradient(162deg, #3c3c3c 10%, #000 68%, #000 100%);

@font-face {
  font-family: "Digital";
  src: url("//db.onlinewebfonts.com/t/8e22783d707ad140bffe18b2a3812529.eot");
  src: url("//db.onlinewebfonts.com/t/8e22783d707ad140bffe18b2a3812529.eot?#iefix") format("embedded-opentype"), url("//db.onlinewebfonts.com/t/8e22783d707ad140bffe18b2a3812529.woff2") format("woff2"), url("//db.onlinewebfonts.com/t/8e22783d707ad140bffe18b2a3812529.woff") format("woff"), url("//db.onlinewebfonts.com/t/8e22783d707ad140bffe18b2a3812529.ttf") format("truetype"), url("//db.onlinewebfonts.com/t/8e22783d707ad140bffe18b2a3812529.svg#Digital-7") format("svg");
}

@mixin inners($color) {
  box-shadow: inset -1px -2px 4px darken($color, 15),
    inset 1px 2px 4px lighten($color, 25);
}
@mixin gradientleft($color) {
  background: linear-gradient(
    162deg,
    lighten($color, 12) 20%,
    $color 68%,
    $color 100%
  );
}

html,
body {
  margin: 0;
  padding: 0;
}
* {
  box-sizing: border-box;
}

.App {
  text-align: center;
  width: 100%;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: $bg-color;
  font-family: "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif;
}

.calc-container {
  width: 300px;
  height: 450px;
  background: white;
  display: flex;
  flex-direction: column;
  padding: 1rem;
  border-radius: 1rem;
  box-shadow: $inner-shadow;
  background: linear-gradient(
    162deg,
    rgba(230, 229, 229, 1) 0%,
    rgba(210, 210, 210, 1) 43%
  );

  .header-wrapper {
    display: flex;
    align-items: center;
    margin: 0.7rem 0.7rem 0.2rem 0;
    .calc-name {
      text-transform: uppercase;
      padding-left: 0.7rem;
      font-family: "Rajdhani", sans-serif;
      font-weight: 700;
      letter-spacing: 2px;
    }

    .solar {
      background: darken($display-color, 5);
      width: 50px;
      height: 20px;

      margin-left: auto;
      box-shadow: inset 0 -2px 4px #cecece, inset 0 1px 15px #000;
      display: flex;
      border-radius: 3px;

      span {
        margin-left: auto;
        width: 1.7px;
        height: inherit;
        background: linear-gradient(
          327deg,
          rgba($display-color, 0.4) 0%,
          rgba(0, 0, 0, 0.5) 100%
        );
      }
    }
  }

  .display-wrapper {
    background: $display-color;
    padding: 0.5rem 1rem;
    border-radius: 5px;
    margin: 0.7rem 0;
    text-align: right;
    box-shadow: $inner-shadow;
    // font-family: "Orbitron", sans-serif;
    font-family: "Digital";
    font-size: 1.5rem;
    overflow: hidden;
    font-weight: bold;
    height: 66.8px;
    .display {
      opacity: 0.8;
    }
    .formula {
      font-size: 1rem;
      opacity: 0.5;
      height: 26.4px;
    }
  }
  h3 {
    margin: 0.7rem 0;
  }
}

.buttons-wrapper {
  padding-top: 1rem;
  display: grid;
  grid-template-areas:
    "ac ac divide multiply"
    "seven eight nine minus"
    "four five six plus"
    "one two three equals"
    "zero zero decimal equals";
  grid-gap: 10px;
  .buttons {
    color: white;
    border-radius: 7px;
    padding: 0.7rem 1rem;
    border: none;
    box-shadow: $inner-shadow;
    background: $top-left-gr-black;
    font-size: 1.3rem;
    cursor: pointer;
    &:focus {
      outline: none;
    }
    &:hover {
      transform: scale(0.97);
    }
  }
  grid-template-columns: repeat(4, 1fr);
  #clear {
    grid-area: ac;
    @include gradientleft(#655168);
    @include inners(#655168);
  }
  #equals {
    grid-area: equals;
  }
  #zero {
    grid-area: zero;
  }
  .nbr {
    @include gradientleft(#1f1d1f);
    @include inners(#1f1d1f);
  }
}

              
            
!

JS

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

class Buttons extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <div className="buttons-wrapper">
        <button
          id="clear"
          className="buttons"
          value="AC"
          onClick={this.props.clearVal}
        >
          AC
        </button>
        <button
          id="divide"
          className="buttons"
          value="/"
          onClick={() => this.props.performOp("/")}
        >
          /
        </button>
        <button
          id="multiply"
          className="buttons"
          value="x"
          onClick={() => this.props.performOp("*")}
        >
          x
        </button>
        <button
          id="seven"
          className="buttons nbr"
          value="7"
          onClick={this.props.typeNumbers}
        >
          7
        </button>
        <button
          id="eight"
          className="buttons nbr"
          value="8"
          onClick={this.props.typeNumbers}
        >
          8
        </button>
        <button
          id="nine"
          className="buttons nbr"
          value="9"
          onClick={this.props.typeNumbers}
        >
          9
        </button>
        <button
          id="subtract"
          className="buttons"
          value="-"
          onClick={() => this.props.performOp("-")}
        >
          -
        </button>
        <button
          id="four"
          className="buttons nbr"
          value="4"
          onClick={this.props.typeNumbers}
        >
          4
        </button>
        <button
          id="five"
          className="buttons nbr"
          value="5"
          onClick={this.props.typeNumbers}
        >
          5
        </button>
        <button
          id="six"
          className="buttons nbr"
          value="6"
          onClick={this.props.typeNumbers}
        >
          6
        </button>
        <button
          id="add"
          className="buttons"
          value="+"
          onClick={() => this.props.performOp("+")}
        >
          +
        </button>
        <button
          id="one"
          className="buttons nbr"
          value="1"
          onClick={this.props.typeNumbers}
        >
          1
        </button>
        <button
          id="two"
          className="buttons nbr"
          value="2"
          onClick={this.props.typeNumbers}
        >
          2
        </button>
        <button
          id="three"
          className="buttons nbr"
          value="3"
          onClick={this.props.typeNumbers}
        >
          3
        </button>
        <button
          id="zero"
          className="buttons nbr"
          value="0"
          onClick={this.props.typeNumbers}
        >
          0
        </button>
        <button
          id="decimal"
          className="buttons"
          value="."
          onClick={this.props.handleDot}
        >
          .
        </button>
        <button
          id="equals"
          className="buttons"
          value="="
          onClick={this.props.handleEquals}
        >
          =
        </button>
      </div>
    );
  }
}

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      curVal: 0,
      formula: "",
      evaluated: false,
      lastResult: ""
    };
  }

  componentDidMount() {
    document.addEventListener("keydown", this.handleKeyDown);
  }

  componentWillUnmount() {
    document.removeEventListener("keydown", this.handleKeyDown);
  }
  clearVal = () => {
    this.setState({
      curVal: 0,
      formula: "",
      lastResult: "",
      evaluated: false
    });
  };

  typeNumbers = (e, val) => {
    // debugger;
    // this.setState({ evaluated: false });
    if (this.state.evaluated === true) {
      this.setState({
        curVal: val || e.target.value,
        formula: val || e.target.value,
        evaluated: false
      });
      
    } else if (
      (val || e.target.value) == 0 &&
      this.state.curVal == 0 &&
      this.state.formula == ""
    ) {
      this.setState({
        formula: 0
      });
      
    } else if (
      //if initial val was 0 but i want another digit to start my formula before clicking operator
      this.state.curVal == 0 &&
      this.state.formula == 0
    ) {
      this.setState({
        curVal: /(0[.0-9].*)$/.test(this.state.curVal)
          ? this.state.curVal + (val || e.target.value)
          : val || e.target.value,
        formula: /(0[.0-9].*)$/.test(this.state.curVal)
          ? this.state.curVal + (val || e.target.value)
          : val || e.target.value
      });
      
    } else {
      this.setState({
        curVal:
          this.state.curVal == "0" || isOperator.test(this.state.curVal)
            ? val || e.target.value
            : this.state.curVal + (val || e.target.value),
        formula:
          this.state.curVal == "0" && (val || e.target.value) == "0"
            ? this.state.formula
            : /([^.0-9]0)$/.test(this.state.formula)
            ? this.state.formula.slice(0, -1) + (val || e.target.value)
            : this.state.formula + (val || e.target.value)
      });
    }
    
  };

  handleKeyDown = e => {
    let { key } = e;

    if (/\d/.test(key)) {
      e.preventDefault();
      this.typeNumbers(null, key);
    } else if (isOperator.test(key)) {
      e.preventDefault();
      this.performOp(key);
    } else if (key === ".") {
      e.preventDefault();
      this.handleDot();
    } else if (key === "Backspace") {
      e.preventDefault();
      this.clearVal();
    } else if (key === "Delete") {
      e.preventDefault();
      this.clearVal();
    }
    if (key === "Enter") {
      key = "=";
      e.preventDefault();
      this.handleEquals();
      console.log(key);
    }
  };

  performOp = nextOperator => {
    if (this.state.evaluated) {
      this.setState({
        formula: this.state.lastResult + nextOperator,
        curVal: nextOperator,
        evaluated: false
      });
      console.log("performop here? nextOperator");
    } else {
      if (endsWithOperator.test(this.state.formula)) {
        this.setState({
          curVal: nextOperator,
          formula: this.state.formula.slice(0, -1) + nextOperator
        });
      } else {
        this.setState({
          curVal: nextOperator,
          formula: this.state.formula + nextOperator
        });
      }
    }
  };

  handleEquals = () => {
    let form = this.state.formula;
    if (endsWithOperator.test(form)) {
      form = form.slice(0, -1);
    }
    form = form.replace(/x/g, "*").replace(/‑/g, "-");
    let calcTotal = Math.round(100000000 * eval(form)) / 100000000;
    this.setState({
      curVal: calcTotal.toString(),
      formula: form.replace(/\*/g, "⋅").replace(/-/g, "‑") + "=" + calcTotal,
      lastResult: calcTotal,
      evaluated: true
    });
    
  };

  handleDot = () => {
    if (this.state.curVal === 0 && this.state.evaluated) {
      this.setState({
        curVal: "0.",
        formula: "0.",
        evaluated: false
      });
    } else if (isOperator.test(this.state.curVal)) {
      this.setState({
        curVal: "0.",
        formula: this.state.formula + "0."
      });
    } else if (!hasDecimal.test(this.state.curVal)) {
      this.setState({
        curVal: this.state.curVal + ".",
        formula: this.state.formula + "."
      });
    }
  };

  render() {
    return (
      <div className="App">
        <div className="calc-container">
          <div className="header-wrapper">
            {" "}
            <div className="calc-name">Reactsio</div>
            <div className="solar">
              <span />
              <span />
              <span />
              <span />
            </div>
          </div>

          <div className="display-wrapper">
            <div className="formula">
              {String(this.state.formula).substring(0, 21)}
            </div>
            <div id="display" className="display">
              {String(this.state.curVal).substring(0, 15)}
            </div>
          </div>
          <Buttons
            clearVal={this.clearVal}
            typeNumbers={this.typeNumbers}
            performOp={this.performOp}
            handleEquals={this.handleEquals}
            handleDot={this.handleDot}
          />
        </div>
      </div>
    );
  }
}
ReactDOM.render(<App />, document.getElementById("root"));

              
            
!
999px

Console