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

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

CSS

              
                *{
  margin:0;
  padding:0;
}
//center a box 
#centerd-box{
  margin-right:auto;
  margin-left:auto;
}
$color_white:white;//Sass variable
#root{
  background-color:lightblue;
  height:100vh;
  display:flex;
  align-items:center;
}

#centerd-box {
  border:1px solid black;
  height: 420px;
  width: 360px;
}
//ShowSections
#inner-ShowSectionsBox {
  height:65px;
  background-color:black;
}

 .show {
  min-height:33.5px;
  border-bottom:1px solid rgba(255,255,255,0.3);
  color:$color_white;
  text-align:right;
  font-size:30px;
}
.show:first-child{
  color:green;
  font-size:20px;
  line-height:33.5px;
  font-family: Mono, MonoItalic;
} 
//CaluclatorSection
#inner-CaluclatorSectionBox table {
  border:1px solid black;
  display:block;
  height: 353px;
  border-collapse: collapse;
}
#inner-CaluclatorSectionBox table tr td {
  border:1px solid black;
  width:100px;
  height: 70px;
  text-align:center;
  font-weight:bold;
  font-size:22px;
  background-color:#131f23;
  color:$color_white;
  font-family:sans-serif;
  cursor:pointer;
}
#inner-CaluclatorSectionBox table tr td:hover {
  color:grey;
  outline:0.5px solid grey;
  box-sizing:border-box;
}
#inner-CaluclatorSectionBox table tr:first-child td:first-child
{
  background-color:#92560e;
}

#inner-CaluclatorSectionBox table tr:nth-child(1) td,
#inner-CaluclatorSectionBox table tr:nth-child(2) td:nth-child(4),
#inner-CaluclatorSectionBox table tr:nth-child(3) td:nth-child(4),
#inner-CaluclatorSectionBox table tr:nth-child(4) td:nth-child(4)
{
  background-color:#4c4f5a;
}
              
            
!

JS

              
                class App extends React.Component {
  constructor(props){
    super(props);
      this.state={
        currentVal:"", //currentVal and currentValShadow has the same value
        result:"0", // get the value from currentVal
        resultUp:"",//get the value from currentValShadow
        currentValShadow:"",//currentVal and currentValShadow has the same value
        reset:false,
        doubtlPoint:"",
        forPoint:""
      }
  }
  getCurrentValue(e){
   let CurrentValue=document.getElementById(e.target.id).innerHTML;//CurrentValue here is for getting th value 1 2 3 4 ...
    this.setState({currentVal:(this.state.currentVal+=CurrentValue)});//update ""+"0"="0";
    if(/^00/.test(this.state.currentVal)){// if currentVal in the beginning  = 00
    this.setState({currentVal:"0"}); // to don't let it appear on the top result in the calculator
    }
     else {
    this.setState({currentValShadow:(this.state.currentValShadow+=CurrentValue)})// currentValShadow is the same as currentVal
    this.setState({result:this.state.currentVal}); // let currentVal appear on the top result
    this.setState({resultUp:this.state.currentValShadow});//let currentVal appear on the bottom result
    this.setState({doublePoint:""});
    }
  }
    reset(){
    this.setState({currentVal:""});
   this.setState({result:"0"});
   this.setState({resultUp:""});
   this.setState({currentValShadow:""});
   this.setState({doublePoint:""});
   this.setState({forPoint:""});
  }
   getOperator(e){
  let CurrentValue=document.getElementById(e.target.id).innerHTML;
   
    (CurrentValue==="x" ? CurrentValue="*":CurrentValue);
     
  /* if(/[+-/*]{1}$/.test(this.state.currentValShadow)){//if two operator
     let changeLastOperator=this.state.currentValShadow.replace(/[+-/*]{1}$/,CurrentValue); // change the pre-last one by the currentone
     CurrentValue=changeLastOperator[changeLastOperator.length-1];//change CurrentValue by the last Operator
     this.setState({currentValShadow:changeLastOperator}); //update currentValShadow by the whole string changeLastOperator 
     this.setState({result:CurrentValue});//show on the bottom result to see just operators
   }
  else {
    */
    this.setState({currentVal:""}); // is empty to not update the result state
    this.setState({result:CurrentValue});
    this.setState({currentValShadow:(this.state.currentValShadow+=CurrentValue)});
    this.setState({resultUp:this.state.currentValShadow});
  
  }
 point(e){
    let CurrentValue=document.getElementById(e.target.id).innerHTML;// get point
    this.setState({forPoint:(this.state.forPoint+=CurrentValue)});
    this.setState({doublePoint:(this.state.doublePoint+=CurrentValue)})// doublePoint state 
    if(this.state.doublePoint.length ===1){ // means that I am clicking one point 
      this.setState({currentVal:(this.state.currentVal+=CurrentValue)});
      this.setState({currentValShadow:(this.state.currentValShadow+=CurrentValue)});
    }
   
 }
   equal(e){
       if(/[+-/*]{1}$/.test(this.state.currentValShadow)){//if two operator
      let toArr=this.state.currentValShadow.split("");
     toArr[toArr.length-1]="";
     let toStr=toArr.join("");
     this.setState({resultUp:eval(toStr)});
     this.setState({result:eval(toStr)});
   }
  else {
    let CurrentValue=document.getElementById(e.target.id).innerHTML;// get = operator
  this.setState({resultUp:(eval(this.state.currentValShadow))}) // get the result at the top result
 this.setState({currentValShadow:eval(this.state.resultUp)});// get the sum in currentValShadow
   this.setState({result:eval(this.state.resultUp)}) // get the sum at bottom result 
     this.setState({forPoint:""});
  }
    }
  componentShouldUpdate(){
    if(this.state.doublePoint.length >1){ // if this condition is true means that I am clicking more point 
     this.setState({doublePoint:""}); // make it to 0 point so nothing runs.
    }
  }
 
  componentWillUpdate(){
    if(/[+-/*]\.$/.test(this.state.currentValShadow)){
   let replaceCurrentShadowLastchar=this.state.currentValShadow.replace(/\.$/,"0.");
    this.setState({currentValShadow:replaceCurrentShadowLastchar});
      this.setState({currentVal:"0."});
      this.setState({result:"0."})
    }
    else if (/\.\d{1,8}\./.test(this.state.currentValShadow)){
     let anotherReplace=this.state.currentValShadow.replace(/\.$/,"");
     this.setState({currentValShadow:anotherReplace});
      this.setState({currentVal:""}); 
      this.setState({result:anotherReplace})
   }
    else if(/[+-/*]{2}$/.test(this.state.currentValShadow)){//if two operator
      let toArr=this.state.currentValShadow.split("");
     toArr[toArr.length-2]="";
     let toStr=toArr.join("");
     this.setState({currentValShadow:toStr});
     this.setState({resultUp:toStr})
   }
   else if(this.state.forPoint==="." && this.state.result==="0"){
    this.setState({currentValShadow:"0."});
     this.setState({currentVal:"0."}); 
     this.setState({forPoint:""});
     this.setState({result:"0."})
     this.setState({resultUp:"0."})
   }
  }
 
  render(){
   return (<div id="centerd-box">
      <ShowSections getSimpleResult={this.state.result}
        getResultUp={this.state.resultUp}/>
      <CaluclatorSection getCurrentValue={this.getCurrentValue.bind(this)}
        reset={this.reset.bind(this)}
        adding={this.getOperator.bind(this)} 
        equality={this.equal.bind(this)}
        substraction={this.getOperator.bind(this)}
        multiplication={this.getOperator.bind(this)}
        dividing={this.getOperator.bind(this)}
        add_point={this.point.bind(this)}/>
    </div>)
  }
}

const ShowSections=(props)=>{
  return(
    <div id="inner-ShowSectionsBox">
      <section  className="show">{props.getResultUp}</section>
      <section  id="display" className="show" >
        {props.getSimpleResult}</section>
    </div>
  )
}
const CaluclatorSection =(props)=>{
  return(
    <div id="inner-CaluclatorSectionBox">
     <table>
       <tr>
         <td onClick={props.reset} id="clear" colspan="2">AC</td>
         <td onClick={props.dividing} id="divide">/</td>
         <td onClick={props.multiplication} id="multiply">x</td>
       </tr>
       
        <tr>
         <td onClick={props.getCurrentValue} id="seven">7</td>
         <td onClick={props.getCurrentValue} id="eight">8</td>
         <td onClick={props.getCurrentValue} id="nine">9</td>
         <td onClick={props.substraction} id="subtract">-</td>
       </tr>
       
        <tr>
         <td onClick={props.getCurrentValue} id="four">4</td>
         <td onClick={props.getCurrentValue} id="five">5</td>
         <td onClick={props.getCurrentValue} id="six">6</td>
         <td onClick={props.adding} id="add">+</td>
       </tr>
       
        <tr>
         <td onClick={props.getCurrentValue} id="one">1</td>
         <td onClick={props.getCurrentValue} id="two">2</td>
         <td onClick={props.getCurrentValue} id="three">3</td>
         <td onClick={props.equality} id="equals" rowspan="2">=</td>
       </tr>
       
       <tr>
         <td onClick={props.getCurrentValue} id="zero" colspan="2">0</td>
         <td onClick={props.add_point}id="decimal">.</td>
       </tr>
     </table>
    </div>
  )
}
ReactDOM.render(<App />,document.getElementById("root"));
              
            
!
999px

Console