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

              
                <link href="https://fonts.googleapis.com/css2?family=Georama&display=swap" rel="stylesheet">

<div id="root"></div>
              
            
!

CSS

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

#calc-box{
  margin: auto;
  margin-top: 10vh;
  border: 3px solid black;
  border-radius: 5px;
  background-color: #4d4d4d;
  width: 300px;
  box-shadow: rgba(0, 0, 0, 0.25) 0px 54px 55px, rgba(0, 0, 0, 0.12) 0px -12px 30px, rgba(0, 0, 0, 0.12) 0px 4px 6px, rgba(0, 0, 0, 0.17) 0px 12px 13px, rgba(0, 0, 0, 0.09) 0px -3px 5px;
}

.screen{
  background-color: #1c1c1c;
  text-align: right;
  font-family: 'Georama', sans-serif;
}

.display-screen{
  padding: 10px;
  color: white;
}

.result{
  padding: 10px;
  color: white;
  font-size: 1.6em;
}

.btn-color-black{
  background: #1c1c1c;
}
.btn-color-green{
  background: #52d978;
}
.btn-color-red{
  background: #d95252;
}
.btn-color-blue{
  background: #5296d9;
}
.btn-color-grey{
  background: #858585;
}

.btn{
  color: white;
  padding: 30px;
  cursor: pointer;
  z-index: 1;
  border: none;
  text-decoration: none;
}

.btn:hover{
   border: 1px solid white;
}

.btn-grid{
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr; 
  grid-template-rows: 1fr 1fr 1fr 1fr 1fr;
  column-gap: 1px;
  row-gap: 1px;
}

#clear{
  grid-column: 1 / 3;
}

#equals{
  grid-row: span 2;
}

#zero{
  grid-column: 1 / 3;
}


              
            
!

JS

              
                import {evaluate, round} from "https://cdn.skypack.dev/mathjs";

const endsWithOperator = /[x+/]$/;
const endsWithNegative = /[-]$/;
const endsWithNegativeSign = /\d[x/+‑]{1}‑$/;

function App(){
  return(
    <div>
      <Calculator/>
    </div>
  )
};

function Calculator(props){
  const [display, setDisplay] = React.useState('0');
  const [input, setInput] = React.useState('');
  const [result, setResult] = React.useState('');
  const [operatorPressed, setOperatorPressed] = React.useState(false);
  const [decimalPressed, setDecimalPressed] = React.useState(false);
  const [isReseted, setIsReseted] = React.useState(true);
  const [secondEqualsPressed, setSecondEqualsPressed] = React.useState(false);
  
  
  //CLEAR EVENT HANDLER -------------------------------------------------------
  const handleClear = () => {
    console.log("ok");
    setResult('');
    setInput('');
    setDisplay('0');
    setIsReseted(true);
    setDecimalPressed(false);
  }
  
 //OPERATION EVENT HANDLER ---------------------------------------------------- 
  const handleOperation = (e) => {
    setDecimalPressed(false);  
    let currentOperator = e.currentTarget.value;
    let currentExpression = display + ' ' + currentOperator;
    
    if(operatorPressed == false)
      {
        setDisplay(currentExpression);
        setInput(currentOperator);
        setOperatorPressed(true);
      }
    
    else if(endsWithNegative.test(display))
      {
        let displayWithoutLastTwoOperators = display.substring(0,display.length-3);
        setDisplay(displayWithoutLastTwoOperators + ' ' + e.currentTarget.value);
        setInput(e.currentTarget.value); 
      }
    
    else if(endsWithOperator.test(display))
      {
        if(currentOperator == "-")
        {
          setDisplay(display + ' ' + currentOperator);
          setInput(currentOperator);
        }
        else
        {
           let displayWithoutLastOperator = display.substring(0,display.length-1);
           setDisplay(displayWithoutLastOperator + ' ' + e.currentTarget.value);
           setInput(e.currentTarget.value);
        }
        
      }
    
  }
  
  //NUMBER EVENT HANDLER ------------------------------------------------------
  const handleNumber = (e) => {
   setOperatorPressed(false);
     
   //If its not reseted, you can keep adding numbers to build a one-or-more-digit single number
   if(isReseted == false)
   {
          //display one single number at a time
          let lastChar = display.charAt(display.length - 1);
          if(lastChar == 'x' || lastChar == '/' || lastChar == '+' || lastChar == '-')
            { 
              setInput(e.currentTarget.value);
              setDisplay(display + ' ' + e.currentTarget.value);
            }
          //If not finished entering the number yet, you can keep adding digits to build it completely
          else
            {
              setInput(input + e.currentTarget.value);
              setDisplay(display + e.currentTarget.value);
            }
   }
   //This condition keeps a number from starting with 0 by clicking the button (you can still start with 0 if you add a decimal point or perform an operation on the default initial value, which is 0)
   else if(isReseted && e.currentTarget.value !== '0')
   {
     setInput(input + e.currentTarget.value);
     setDisplay(e.currentTarget.value);
     setIsReseted(false);
   }
   
  }
   
 //EQUALS EVENT HANDLER -------------------------------------------------------
  const handleEquals = () => {
    let expression = display.replace('x', '*');
    //MathJs Library evaluate() to avoid eval() security issues
    //https://dev.to/spukas/everything-wrong-with-javascript-eval-35on
    let totalResult = round(evaluate(expression),4);
    console.log("Result: "+totalResult.toString());
    setResult(totalResult);
    setInput('');
    setDisplay(totalResult);  
    setDecimalPressed(false);
  }
    
 //DECIMAL EVENT HANDLER -----------------------------------------------------
  const handleDecimal = () => {
     
     if(decimalPressed == false)
      {
        setDisplay(display.concat("."));
        setInput(display.concat("."));
        setDecimalPressed(true); 
        
      }
   
   }
    
   return (
      <div id="calc-box">
        <div  className="screen">
          <div id='display' className="display-screen">{display}</div>
          <div className="result">{input}</div>
        </div>
        <div className="btn-grid">
          <Button name='clear' simbol='AC' color='red' onClick={handleClear}></Button>
          <Button name='divide' simbol='/' color='green' onClick={handleOperation}></Button>
          <Button name='multiply' simbol='x' color='green' onClick={handleOperation}></Button>
          <Button name='seven' simbol='7' color='black' onClick={handleNumber}></Button>
          <Button name='eight' simbol='8' color='black' onClick={handleNumber}></Button>
          <Button name='nine' simbol='9' color='black' onClick={handleNumber}></Button>
          <Button name='subtract' simbol='-' color='green' onClick={handleOperation}></Button>
          <Button name='four' simbol='4' color='black' onClick={handleNumber}></Button>
          <Button name='five' simbol='5' color='black' onClick={handleNumber}></Button>
          <Button name='six' simbol='6' color='black' onClick={handleNumber}></Button>
          <Button name='add' simbol='+' color='green' onClick={handleOperation}></Button>
          <Button name='one' simbol='1' color='black' onClick={handleNumber}></Button>
          <Button name='two' simbol='2' color='black' onClick={handleNumber}></Button>
          <Button name='three' simbol='3' color='black' onClick={handleNumber}></Button>
          <Button name='equals' simbol='=' color='blue' onClick={handleEquals}></Button>
          <Button name='zero' simbol='0' color='black' onClick={handleNumber}></Button>
          <Button name='decimal' simbol='.' color='grey' onClick={handleDecimal}></Button>
        </div>
      </div>
    )
 };


function Button(props){
    return (
      <button id={props.name} className={"btn btn-color-"+props.color} value={props.simbol} onClick={props.onClick}>{props.simbol}</button>
    )
  };

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

Console