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

CSS

              
                
body{
  display : grid;
  box-sizing: border-box;
  background: #D980FA;
  box-sizing: border-box;
}

.container{
  margin: auto;
  width: 25%;
  background:#833471;
  border: 4px solid #833471;
  display: grid;
  grid-gap: 1px;
  grid-template-columns: repeat(4, 1fr);
  grid-auto-rows: 50px;
  justify-items: stretch;
  align-items: stretch;
  grid-template-rows: repeat(autofit, minmax(200px, 1fr));
  grid-template-areas: 
    "ex ex ex ex"
    "d d d d "
    "ce ce divide multi"
    "seven eight nine minus"
    "four five six plus"
    "one two three equal"
    "zero zero decimal equal";
}

#expr { 
  grid-area: ex;
  background: #000;
  color: #fff;
  justify-content: flex-end;
}
#display {
  grid-area: d;
  background: #000;
  color: #fff;
  justify-content: flex-end;
}
#clear {
  grid-area: ce;
}
#zero {
  grid-area: zero;
}
#one {
  grid-area: one;
}
#two {
  grid-area: two;
}
#three {
  grid-area: three;
}
#four {
  grid-area: four;
} 
#five {
  grid-area: five;
}
#six {
  grid-area: six;
}
#seven {
  grid-area: seven;
}
#eight {
  grid-area: eight;
}
#nine {
  grid-area: nine;
}
#decimal {
  grid-area: decimal;
}
#add {
  grid-area: plus;
}
#subtract {
  grid-area: minus;
}
#multiply {
  grid-area: multi;
}
#divide {
  grid-area: divide;
}
#equals {
  grid-area: equal;
}


.container div{
  width: 100%;
  height: 100%;
  display: grid;
  justify-content: center;
  align-items: center;
  background: #1B1464;
  color: #fff;
}

.container div:hover{
  outline: 1px #EE5A24 solid;
  
}
              
            
!

JS

              
                console.clear();
//action types
const ADD = "ADD";
const SUBTRACT = 'SUBTRACT';
const MULTIPLY = 'MULTIPLY';
const DIVIDE = 'DIVIDE';
const DECIMAL = 'DECIMAL';
const VALIDATE = 'VALIDATE';
const CLEAR = 'CLEAR'; 
const EVAL = 'EVAL';
const INPUT = 'INPUT';
let re = /[\+ \- \* \/ ]$/;

let defaultState ={
  input: 0,
  equation: '',
  result : 0,
  isResult: false
} 
const rootReducer = (state=defaultState, action)=>{
  switch(action.type){
    case INPUT : 
      
      return state = {...state, 
                      input: typeof(state.input)=== 'number' ? (state.input * 10 + action.num):state.input + action.num , 
                      equation: !state.isResult ? state.equation + action.num :(state.input * 10 + action.num).toString() , 
                      isResult: false  }
    case ADD:
      
      return state={...state, equation: state.isResult ? state.result + ' + ' : state.equation.match(re) !== null ? state.equation.slice(0, state.equation.length -2) + ' + ': state.equation + ' + ', input: 0, isResult: false};
    case SUBTRACT:
      
      return state={...state, equation: state.isResult ? state.result + ' - ' : state.equation.match(re) !== null ? state.equation.slice(0, state.equation.length -2) + ' - ':  state.equation + ' - ', input: 0, isResult: false};
    case MULTIPLY:
      
      return state={...state, equation: state.isResult ? state.result + ' * ' : state.equation.match(re) !== null ? state.equation.slice(0, state.equation.length -2) + ' * ': state.equation + ' * ', input: 0, isResult: false};
    case DIVIDE:
      
      return state={...state, equation: state.isResult ? state.result + ' / ' :
                    state.equation.match(re) !== null ? state.equation.slice(0, state.equation.length -2) + ' / ': state.equation + ' / ', input: 0, isResult: false};
    case DECIMAL :
      
      return state={...state, 
                    input: state.input.toString().includes('.') ? state.input : state.input + '.', 
                    equation : state.input.toString().includes('.') ? state.equation  : state.equation.toString() + '.'}
    case EVAL:
      
      return state={...state, 
                    result: eval(state.equation.match(re) !== null ? state.equation.slice(0, state.equation.length -2) :state.equation), 
                    equation: state.equation + ' = ' + eval(state.equation.match(re) !== null ? state.equation.slice(0, state.equation.length -2) :state.equation).toString(), input: 0, isResult : true};
    case CLEAR:
      
      return state={...state, input: 0, result: 0, equation: ''}
    default: 
      return state;
      
      
  }
}
const {createStore} = Redux;
// console.log(createStore);
const store = createStore(rootReducer);
// console.log(store.getState());


// action creator
const inputAction = (num)=>{
  return {
    type : INPUT,
    num
  }
};
const addAction = (num)=>{
  return {
    type: ADD,
    num
  }
};
const subtractAction = (num)=>{
  return {
    type: SUBTRACT,
  }
};
const multiplyAction = (num)=>{
  return {
    type: MULTIPLY,
  }
};
const divideAction = (num)=>{
  return {
    type: DIVIDE,
  }
};

const decimalAction = ()=>{
  return {
    type : DECIMAL
  }
}


const clearAction = ()=>{
  return {
    type: CLEAR
  }
}
const evalAction = ()=>{
  return {
    type: EVAL
  }
}


const mapStateToProps = ({result, input, equation, isResult})=>{
  return {
    result, 
    input, 
    equation,
    isResult
  }
}

const mapDispatchToProps = (dispatch)=>{
  return {
    getInput : (e)=>{
      dispatch(inputAction(parseInt(e.target.innerText, 10)));
    },
    add : ()=>{
      dispatch(addAction());
    },
    subtract : ()=>{
      dispatch(subtractAction());
    },
    multiply : ()=>{
      dispatch(multiplyAction());
    },
    divide : ()=>{
      dispatch(divideAction());
    },
    decimal : ()=>{
      dispatch(decimalAction())
    },
    evaluate : ()=>{
      dispatch(evalAction())
    },
    clear : ()=>{
      dispatch(clearAction());        
    }
  }
}



class Keypad extends React.Component{
  constructor(props){
    super(props);
  }
  render(){
    return(
      <div class='container'>
        <div id='zero' onClick={this.props.getInput}>0</div>
        <div id='one' onClick={this.props.getInput}>1</div>
        <div id='two' onClick={this.props.getInput}>2</div>
        <div id='three' onClick={this.props.getInput}>3</div>
        <div id='four' onClick={this.props.getInput}>4</div>
        <div id='five' onClick={this.props.getInput}>5</div>
        <div id='six' onClick={this.props.getInput}>6</div>
        <div id='seven' onClick={this.props.getInput}>7</div>
        <div id='eight' onClick={this.props.getInput}>8</div>
        <div id='nine' onClick={this.props.getInput}>9</div>
        <div id='decimal' onClick={this.props.decimal}>.</div>
        <div id='equals' onClick={this.props.evaluate}>=</div>
        <div id='clear' onClick={this.props.clear}>CE</div>
        <div id='add' onClick={this.props.add}>+</div>
        <div id='subtract' onClick={this.props.subtract}>-</div>
        <div id='multiply' onClick={this.props.multiply}>*</div>
        <div id='divide' onClick={this.props.divide}>/</div>
        <div id='expr'>{this.props.equation}</div>
        <div id = 'display'> {this.props.isResult ? this.props.result : this.props.input}</div>
      </div> 
    )
  }
}

class App extends React.Component{
  constructor(props){
    super(props);
  }
  
  render(){
    return(
      <div>
        <KeypadContainer />
      </div>
    )
  }
}


const {Provider, connect} = ReactRedux;

const KeypadContainer = connect(mapStateToProps , mapDispatchToProps)(Keypad);

ReactDOM.render(
  <Provider store={store}><App /></Provider>, 
  document.querySelector('#root'));
              
            
!
999px

Console