Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
              
            
!

CSS

              
                #root{
  height:100vh;
  display:flex;
  align-items:center;
  justify-content:center;
  background-color:grey;
}

#mainDiv{
  color: cyan;
  min-width: 273px;
  min-height: 448px;
  padding: 15px 5px 5px 5px; 
  border-radius: 22px;
  background: linear-gradient(145deg, #1a1b1c, #161717);
  box-shadow:  30px 30px 60px #0a0b0b,
             -35px -30px 60px white;
}
#wrapperDiv{
  display: flex;
  box-sizing: border-box;
}
#display{
  display:flex;
  flex-direction: row-reverse;
  border-radius:15px;
  padding: 15px 15px;
  width:96%;
  margin:15px auto;
  font-size:1.3rem;
  font-weight:bold;
  
  background: #18191A;
box-shadow: inset 5px 5px 12px #0a0b0b,
            inset -5px -5px 12px #262729;
}
#nums{
  padding: 5px;
  width: 74%;
  height:350px;
  display:grid;
  grid-template-columns: auto auto auto;
  grid-gap: 10px;
  
}
button{
  border-radius: 50% !important;
  outline:none;
  border: none;
  background: linear-gradient(145deg, #1a1b1c, #161717);
  box-shadow:  23px 23px 36px #0a0a0a,
             -23px -23px 36px #26282a;
  color:cyan;
  margin: 15px;
  font-size:1.3rem !important;
  font-weight: bold;
  box-sizing:border-box;
  
}

button:active{  
  background: #18191A;
  box-shadow: inset 5px 5px 12px #0a0b0b,
            inset -5px -5px 12px #262729;
}
button:hover{
  color:lime;
}
.ac:hover{
  color: Red;
}
#clear{
  grid-column-start: 1;
  grid-column-end: 4;
  border-radius: 20px!important;
  height: 90%;
  width: 66%;
}
#acWrapper{
  grid-column-start: 1;
  grid-column-end: 4;  
}
#zero{  
  grid-column-start: 1;
  grid-column-end: 3;   
  border-radius: 25px !important;
}
#myOperators{
  display:grid;  
  width: 26.5%;  
  grid-gap: 5px;
  padding:5px;
}
              
            
!

JS

              
                // Variables
const nums = [7, 8, 9, 4, 5, 6, 1, 2, 3, 0];
const ops = ["/", "*", "-", "+"];
const ids = {
  7: "seven",
  8: "eight",
  9: "nine",
  4: "four",
  5: "five",
  6: "six",
  1: "one",
  2: "two",
  3: "three",
  0: "zero",
  "/": "divide",
  "*": "multiply",
  "-": "subtract",
  "+": "add"
};

class App extends React.Component {
  constructor(props) {
    super(props);
  }
  state = {
    lastPressed: undefined,
    display: "0",
    operation: undefined
  };

  handleClick = (e) => {
    const { display, operation, lastPressed } = this.state;
    const { innerText } = e.target;
    switch (innerText) {
      case "AC": {
        this.setState({
          display: "0"
        });
        break;
      }

      case ".": {
        const splitted = display.split(/[\+\-\*\/]/);
        const last = splitted.slice(-1)[0];

        if (!last.includes(".")) {
          this.setState({
            display: display + "."
          });
        }
        break;
      }

      case "=": {
        const answer = eval(display);
        this.setState({
          display: answer
        });
        break;
      }
      default: {
        let e = undefined;
        // check for other op
        if (ops.includes(innerText)) {
          if (ops.includes(lastPressed) && innerText !== "-") {
            // oh boii...
            const lastNumberIdx = display
              .split("")
              .reverse()
              .findIndex((char) => char !== " " && nums.includes(+char));
            e =
              display.slice(0, display.length - lastNumberIdx) +
              ` ${innerText} `;
          } else {
            e = `${display} ${innerText} `;
          }
        } else {
          e = display === "0" ? innerText : display + innerText;
        }

        this.setState({
          display: e
        });
      }
    }

    this.setState({
      lastPressed: innerText
    });
  };

  render() {
    const { display } = this.state;
    return (
      <div id="mainDiv">
        <div id="display">{display}</div>
        <div id="numPad">
          <div id="wrapperDiv">
            <div id="nums">
              <div id="acWrapper">
                <button
                  className="big-h light-grey ac"
                  onClick={this.handleClick}
                  id="clear"
                >
                  AC
                </button>
              </div>
              {nums.map((num) => (
                <button id={ids[num]} key={num} onClick={this.handleClick} className="numbers">
                  {num}
                </button>
              ))}

              <button onClick={this.handleClick} id="decimal" className="dot">
                .
              </button>
            </div>

            <div id="myOperators">
              {ops.map((op) => (
                <button id={ids[op]} key={op} onClick={this.handleClick} className="operators">
                  {op}
                </button>
              ))}

              <button onClick={this.handleClick} id="equals">
                =
              </button>
            </div>
          </div>
        </div>
      </div>
    );
  }
}

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

              
            
!
999px

Console