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://cdnjs.cloudflare.com/ajax/libs/mathjs/10.6.3/math.js" integrity="sha512-UC6Hlj+sY9v3zWZ07q8WmJhG66h5AvEIIu9cytjXbd7w4AS9uZMF9HNQiaEsw20MTlFU4WFlSuD6VTTmYolISQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<script crossorigin src="https://unpkg.com/react@17.0.0/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@17.0.0/umd/react-dom.production.min.js"></script>
<div id="app"></div>
              
            
!

CSS

              
                *,
*:before,
*:after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.wrapper {
  background-color: black;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  width: 100vw;
}

.deleteButtons {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-around;
} 
  
.delButton {
  border-radius: 50%;
  box-shadow: -1px -1px 10px blueviolet, inset -1px -1px 10px  blue;
  border: 1px solid blueviolet;
}

.delButton:hover {
  opacity: 0.6;
}

.buttons-container {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-around;
}

.button {
    height: 5rem;
    width: 22%;
    padding: 0;
    margin: 2px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid;
    border-radius: 25%;
    user-select: none; 
    box-shadow: -1px -1px  10px  blueviolet,  inset -1px -1px 10px  blue; 
    font-size: 1.5rem;
}
.button:hover {
    opacity: 0.6;
}

#display {
    height: 3.5rem;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    flex-wrap: wrap;
    color: black;
    font-size: 2rem;
    background-color: white;
    border-radius: 25px;
    box-shadow: -1px -1px 10px blueviolet, inset -3px -3px 10px  blue;
    border: 1px solid blueviolet; 
}

.buttons-disabled {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-around;
}
.disabledButton {
  border-radius: 50%;
  color: white;
  box-shadow: -1px -1px 4px blueviolet,  inset -1px -1px 10px  blue;
  border: 1px solid blueviolet;
  pointer-events: none;
}
              
            
!

JS

              
                
import { useState, useEffect} from "https://cdn.skypack.dev/react@17.0.1";
const values = [
    {
      id:'seven',
      value: '7',
      key: '7'
    },
    {
      id:'eight',
      value: '8',
      key: '8'
    },
    {
      id:'nine',
      value: '9',
      key: '9'
    },
    {
      id:'multiply',
      value: '*',
      key: '*'
    },
    {
      id:'four',
      value: '4',
      key: '4'
    },
    {
      id:'five',
      value: '5',
      key: '5'
    },
    {
      id:'six',
      value: '6',
      key: '6'
    },
    {
      id:'divide',
      value: '/',
      key: '/'
    },
    {
      id:'one',
      value: '1',
      key: '1'
    },
    {
      id:'two',
      value: '2',
      key: '2'
    },
    {
      id:'three',
      value: '3',
      key: '3'
    },
    {
      id:'decimal',
      value: '.',
      key: '.'
    },
    {
      id:'zero',
      value: '0',
      key: '0'
    },
    {
      id:'add',
      value: '+',
      key: '+'
    },
    {
      id:'subtract',
      value: '-',
      key: '-'
    },
    {
      id:'equals',
      value: '=',
      key: '='
    }
  ];

function App() {
  
  const [input, setInput] = React.useState('0');
  const [display, setDisplay] = React.useState('0');
  const [evaluation, setEvaluation] = React.useState(false);

//validation
  function removezerosdecimal(str) {
    const regex = /(?<=\D)0+\./g;
    let result = str.replace(regex, '0.');
    return result;
  };

  function removezerostart(str) {
    const regex = /^0+(?=\d+)/g;
    let result = str.replace(regex, '');
    return result;
  };

  function removeotherzero(str) {
  const regex = /(?<=[-+*/])0+/g;
  let result = str.replace(regex, '0');
    return result;
  };

  function removedecimals(str) {
  let regex = /(?<=\d+\.\d*)\./g;
  let result = str.replace(regex, '');
  return result;
  };
  function removedecimalinoperator(str) {
  let regex = /(?<=[-+*/])\.|\.(?=[-+*/])/g;
  let result = str.replace(regex, '');
  return result;
  };

  function removeoperators(str) {
  let regex1 = /[-+*/]+(?=[+*/])/g;
  let result1 = str.replace(regex1, '');
  let regex2 = /\-\-/g;
  let result2 = result1.replace(regex2, '-');
  return result2;
  };

  function validateInput(str) {
    let zerostart = removezerostart(str);
    let zerodecimal = removezerosdecimal(zerostart);
    let otherzeros = removeotherzero(zerodecimal);
    let doubledecimal = removedecimals(otherzeros);
    let decimalinoperator = removedecimalinoperator(doubledecimal);
    let result = removeoperators(decimalinoperator);
    return result;
  };
  React.useEffect(() => {
    setInput(validateInput(input));
  },[input])
  function displaylast(val) {
    let arr = val.split('');
    let lastdigits = arr.slice(-15).join('');
    return lastdigits;
  };
  React.useEffect(() => {
    setDisplay(displaylast(input));
  })


  
  //Evaluation
  function removelastoperators(str) {
    let regex = /\D+$/;
    let result = str.replace(regex, '');
    return result;
  };
  
  function decimalSet(val) {
    let parsed = Number.parseFloat(val);
    let setdecimal = parsed.toFixed(4);
    let result = setdecimal.toString();
    return result;
  };
   function removeZerodecimal(str) {
    let pattern = /(\.0+$)|((?<=\.\d+)0+$)/;
    let result = str.replace(pattern, '');
    return result;
  };
  
  function evaluateInput(str) {
    let nolastOperator = removelastoperators(str)
    let value = eval(nolastOperator);
    let roundnumber = decimalSet(value);
    let result = removeZerodecimal(roundnumber)
    return result;
  };



//Handle input
  const addInput = val => {
  let regex1 = /[-+*/]/;
  let regex2 = /\d|./;
    if (val === '=') {
      setInput(evaluateInput(input));
      setEvaluation(true);
    } else if (regex1.test(val)) {
      setInput(input+val);
      setEvaluation(false);
    } else if (regex2.test(val) && !evaluation) {
      setInput(input + val);
      setEvaluation(false);
    } else if (regex2.test(val) && evaluation) {
      setInput(val);
      setEvaluation(false);
    }
  };

  

//Clear the input
  const handleClear =() => {
    setInput('0');
  };
//Delete last input
  function deleteinput(input) {
    if(input.length >1) {
      let result = input.slice(0,-1);
      setInput(result);
    }
  };

  return (
    <div className='wrapper container-fluid'>
      <div className='=container'>
        <div className='row screen'>
          <Display  id='display' input={input} display={display} handleChange={()=>validateInput(input)}/>
        </div>
        <div className='row deleteButtons p-1'>
          <button className='col-6 delButton' id='clear' onClick={handleClear}>AC</button>
           <button className='col-6 delButton' id='del' onClick={()=>deleteinput(input)}>C</button>
        </div>
        <div className='row buttons-container'>
          {values.map((val) => <Button key={val.key} id={val.id} handleClick={addInput}>{val.value}</Button> )}
        </div>
        <div className='row buttons-disabled p-1'>
          <button className='col-6 disabledButton' aria-disabled="true">-</button>
          <button className='col-6 disabledButton' aria-disabled="true">-</button>
        </div>
      </div>
    </div>
  )
};

function Display({display, input, handleChange }){
  return (
      <div id="display" className=" lead form-control" onChange={()=>handleChange(input)}>
      {display}
      </div>
  )
};

function Button(props) {
    return (
        <button className="button" id={props.id} onClick={() => props.handleClick(props.children)}>
            {props.children}
        </button>
    )
};

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

Console