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 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

              
                <h1>Simple React Calculator</h1>

<div id="app"></div>

              
            
!

CSS

              
                html, body{
  height:100vh;
  background-color:grey;
  display:flex;
  flex-direction: column;
}

body{
  display:flex;
  align-items: center;
}

#app {
  background-color: white;
  height:200px;
  display:flex;
  width:280px;
  justify-content:center
}

.calc {
  display:flex;
  width:281px;
  flex-direction: column;
  align-items: center;
  margin-bottom: 0px;
  position:relative;
  z-index:1; 
  background-color:#424654;
  opacity: 0.97;

}

.filter {
  display:flex;
  width:280px;
  flex-direction: column;
  align-items: center;
  margin-bottom: 0px;
  background-color:white;
  position:relative;
  z-index:2;  
}

button {
  margin: 10px;
  width:50px;
  height: 40px;
  border-radius: 0px;
  border: none;
  background:none;
  color: white;
  font-family: roboto;
  font-size:20px;
}

button:hover {
  cursor:pointer;
}

.pad {
  display:flex;
  flex-direction:column;
  width:330px;
  align-items:center;
}

.display{
  text-align:right;
  margin: 0px 0px 10px 45px;
  font-size: 18px;
  font-family: Roboto;
  overflow: hidden;
  width: 250px;
}

.result {
  text-align:right;
  margin: 10px 35px 5px;
  font-size: 40px;
  font-family: Roboto;
  display: flex;
  justify-content: flex-end;
}

.dec{
  margin-top: 4px;
  font-size: 20px;
}

.return {
  background-color:#F28A4F;
  height:60px;
  width:280px;
  margin: 0px;
  z-index:1;
  font-size: 30px;

}

.lastrow {
  display:flex;
  justify-content:flex-end;
  margin-right: 50px;
  width:100%;
}

h1{
  font-family:roboto;
  color: white;
}


.footer {
  margin-top: 20px;
  color: white;
  font-family:roboto; 
  text-align:center;
}

.afterpad{
  background-color: blue;
  z-index:2;
  display:flex;
  align-items:center;
  justify-content:center;
  position:relative;
  width:330px;
  background-color:#424654;
  box-shadow: 4px 4px 10px -2px #000000;
}
              
            
!

JS

              
                var Calc = React.createClass({
   
  getInitialState: function(){
      return {
        display: 0,
        result: 0,
        decimals: ".00"
      }
    },
  
  setResult: function(updates){
    this.setState(updates)
  },
  
  render: function(){
    var result = this.state.result;
    var display = this.state.display;
    var decimals = this.state.decimals;
    
    return(
      <div>
        <Result result={result} decimals={decimals}/>
      <Display display={display} />
        <Nums setResult={this.setResult}/>
        <Footer />
        </div>
    )
  }
});

var op = []; //array for storing numbers
var decimal = true; // to access decimal dot

var Nums = React.createClass({
   
  pusher: function (event) { //will add the button value to op
   event.preventDefault();
     var num = event.target.value;
     
     if (op.length <=23) {
     op += num;       
      this.props.setResult({display: op})
     } else {
       this.props.setResult({display: '# of max chars reached'})
     }
     
  },
  
  pusher2: function (event) { // to only have 1 operation chained
    event.preventDefault();
    var num = event.target.value;
    var last = op.charAt(op.length-1);
    
    if(op.length <= 23){
    
       if ((last === "+") || (last === '*') || (last === '-') || (last ==='/') || (last ==='.')) { 
       } else {
         op += num;
         decimal = true;
         this.props.setResult({display: op})
       }
    } else {
      this.props.setResult({display: '# of max chars reached'})
    }
  },
  
  pusher3: function (event){
    event.preventDefault();
    var num = event.target.value;
      if (op.length <=23) {
     op += num;     
     op = op.replace(/\-+/g, '-')
        decimal=true;
      this.props.setResult({display: op})
     } else {
       this.props.setResult({display: '# of max chars reached'})
     }

  },
  
  pusher4: function(event){ // function for the '.'
    event.preventDefault();
    var num = event.target.value;
    if (op.length <= 23) {
    if (decimal) {
    op += num;
    this.props.setResult({display: op})
    decimal = false; // when '.', sets decimal to false 
    }
    }
     else {
     this.props.setResult({display: '# of max chars reached'})
     }
    
  },
  
  result: function(event){ // will execute the operation inside op
    event.preventDefault();
    
    var result = eval(op).toFixed(2);
    var ind = result.indexOf('.');
    
    if (String(result).length <= 11) {
    this.props.setResult({result: result.slice(0,ind),
                         decimals: result.slice(ind)})
    
    }
    else {
      this.props.setResult({result: 'error', decimals:""})
    }
  },
  
  reset: function(event){
    event.preventDefault();
    op=[];
    decimal = true;
    this.props.setResult({result: 0, display:0, decimals:'.00'});
  },
  
  delete: function(event){
    event.preventDefault();
    var test = false; // to test if im next to delete a '.'
    op = op.slice(0,-1);
    this.props.setResult({display: op})
    console.log(op.indexOf(op.length));
    if(op.charAt(op.length-1) === '.'){ //i know the next char is a '.'
      test= true;
      }
    if(test && (op.charAt(op.length-2) !== '.')){ // activates decimal when i delete a '.'
      decimal = true;
      test = false;
      console.log(test)
    }
    
  },
  
  render: function(){
    return(
      <div className="pad">
        <div className="afterpad">
          <div className='filter'>
    <form className="calc">
    <div>
      <button onClick={this.reset} value={0}> C </button>
      <button onClick={this.delete}> DEL </button> 
      </div>
      
    <div>
    <button onClick={this.pusher} value={1}>1</button> 
    <button onClick={this.pusher} value={2}>2</button> 
    <button onClick={this.pusher} value={3}>3</button> 
    <button onClick={this.pusher2} value='+'>+</button>
    </div>
      
    <div>
    <button onClick={this.pusher} value={4}>4</button> 
    <button onClick={this.pusher} value={5}>5</button> 
    <button onClick={this.pusher} value={6}>6</button> 
    <button onClick={this.pusher3} value='-'>-</button>
    </div>
      
    <div>
    <button onClick={this.pusher} value={7}>7</button> 
    <button onClick={this.pusher} value={8}>8</button> 
    <button onClick={this.pusher} value={9}>9</button> 
    <button onClick={this.pusher2} value='*'>*</button>
    </div>
      
    <div className='lastrow'>
    <button onClick={this.pusher} value={0}>0</button> 
    <button onClick={this.pusher4} value='.'>.</button> 
    <button onClick={this.pusher2} value='/'>/</button>
    </div>
    </form> 
          </div>
        </div>
                      <button className="return" onClick={this.result}>=</button> 
        </div>
    )
  }
});

var Display = React.createClass({
  render: function(){
    return(
    <div className="display">
       {this.props.display} 
    </div>
    )
  }
});


var Result = React.createClass({
  render: function (){
    return (
    <div className='result'>
    <div> {this.props.result} </div>
      <div className="dec">{this.props.decimals}</div> 
       </div>
    )
  }
});

var Footer = React.createClass({
  render: function(){
    return(
    <div className="footer"> Created by Jose L Mortola </div>
    )
  }
});

ReactDOM.render(<Calc />,
document.getElementById('app')
);




              
            
!
999px

Console