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

              
                
<div id="root" style="height: 100%"> hello div</div>
<script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>


              
            
!

CSS

              
                
@import url('https://fonts.googleapis.com/css?family=Quicksand:500|Kaushan+Script|Source+Code+Pro:600|Abril+Fatface|Audiowide|Comfortaa:700|Kelly+Slab&display=swap');


body,
html {
  height: 100%;
  margin: 0px;
  font-family: "Raleway", sans-serif;
}
.themeOne {
    --shade1: #f4f1af;
    --shade2: #e7e283;
    --shade3: #dbd55b;
    --shade4: #dedede;
}

.themeTwo {
    --shade1: #aadff6;
    --shade2: #7ec9e9;
    --shade3: #57b5dd;
    --shade4: #32a1d0;
    --grey1: #dedede;
}

.themeThree {
  --shade1: #c3cacf;
  --shade2: #9ab1c2;
  --shade3: #759bb6;
  --shade4: #5385a9;
   --grey1: #dedede;
}

.themeFour {
  --shade1: #e6e6e6;
  --shade2: #dedede;
  --shade3: #d1d1d1;
  --shade4: #c7c7c7;
   --grey1: #dedede;
}

.themeFive {
  --shade1: #466b8c;
  --shade2: #8c5f46;
  --shade3: #8c8346;
  --shade4: #79468c;
   --grey1: #dedede;
}
@mixin border-radius($a, $b, $c, $d) {
    -webkit-border-radius: $a $b $c $d;
    -moz-border-radius: $a $b $c $d;
    -ms-border-radius: $a $b $c $d;
    border-radius: $a $b $c $d;
}
@mixin height-calc($percentage, $value) {
    height: -moz-calc(#{$percentage} - #{$value});
    /* WebKit */
    height: -webkit-calc(#{$percentage} - #{$value});
    /* Opera */
    height: -o-calc(#{$percentage} - #{$value});
    /* Standard */
    height: calc(#{$percentage} - #{$value});
}
.outerDiv {

    height: 100%;
    width: 100%;
    background-color: var(--shade1);
    transition: all .3s ease-in-out;
}
.innerDiv {
    background: white;
    padding: 0px;

    position: relative;
    min-width: 200px;
    min-height: 200px;
    @include border-radius(9px, 9px, 7px, 7px)
}
.header {

  position: relative;
  top: 0;
  left: 0;
  width: 100%;
  background-color: var(--shade3);
  @include border-radius(7px, 7px, 0px, 0px);
  transition: all .3s ease-in-out;
}

.title {
  width: 100%;
  padding-left: 10px;
  font-size: 1.7em;
  font-family: 'Kaushan Script', cursive;
  color: var(--shade2)
}

.textField {
  box-shadow: inset 0px 0px 7px 7px rgba(0, 0, 0, 0.1);
  height: 1.7em;
  background-color: var(--grey1);
  text-align: right;
  padding-right: 5px;
  @include border-radius(3px, 3px, 3px, 3px);
    margin: 8px 5px 8px 5px;
    border: 1px solid black;
    transition: all .3s ease-in-out;
}

.button {
  width: 2.1em;
  padding: 0 0 0 3;
 
}

.button:focus {
  outline: none;
}

.button:hover{
  box-shadow: inset 0 0 1em rgba(255, 255, 255, .4);
  cursor: default; 
}

.input {
  background-color: var(--shade2);
}

.evaluate {
  background-color: var(--shade4);
}

.clear {
  background-color: var(--shade4);
}


              
            
!

JS

              
                

const {render} =  ReactDOM

const {Provider, connect} = ReactRedux; 
const {createStore} = Redux;



const themes = ["themeOne", "themeTwo", "themeThree", "themeFour", "themeFive"];

// Section for setting up state
const defaultState = {
	theme: 4,
  register: "0",
  input: "0",
  lastAction: ""
}


const INPUT = 0;
const OPERATION = 1;
const EQUALS = 2;
const CLEAR = 3;
const MULTIPLY = 4;
const DIVIDE = 5;
const ADD = 6;
const SUBTRACT = 7;

const BUTTON_CLASSES = ["input", "evaluate", "evaluate", "clear"];

const messageReducer = (state=defaultState, action) => {

  let myState = {...state};
  switch(action.type){
    case INPUT:
      if(action.integer =='.' && myState.input.includes('.'))
        break;
      if(myState.input != "0")
        myState.input = myState.input + action.integer;
      else if(action.integer != "0")
        myState.input = action.integer;
      break;
    case CLEAR:
      if(myState.input =="0"){
        myState.lastAction = "";
        myState.register = "0";
      }
      myState.input = "0";
      break;
    case EQUALS:
      if(myState.lastAction != ""){
        let toEval = myState.register + myState.lastAction + myState.input;
       // console.log("going to evaluate the following: " + toEval);
        myState.input = eval(toEval);
        myState.register = "0";
        myState.lastAction = "";
      }
      break;
    case OPERATION:
      if(myState.input == "0" && myState.register != "0"){
        if(myState.lastAction != "" && !myState.lastAction.includes("-") && action.operation == "-"){
          myState.lastAction = myState.lastAction + " " + action.operation;
        }else{
          myState.lastAction = action.operation;
        }
      //  console.log("still deciding what action to take:  " + myState.lastAction);
        break;
      }
      if(myState.register == "0"){
        myState.register = myState.input;
        myState.lastAction = action.operation;
        myState.input = "0";
      }else{
        let toEval = myState.register + myState.lastAction + myState.input;
      //  console.log("going to evaluate the following: " + toEval);
        myState.register = eval(toEval);
        myState.lastAction = action.operation;
        myState.input = "0";
      }
      break;


    default:
      break;
  }
	return myState;
}

const store = createStore(messageReducer);

// end section for setting up redux

/////// setup for Header 
const Header = (props) =>{
  return(
      <div className="header">
          <div className="title">{props.title}</div>
      </div>
  )
}

/////// end setup for header
const tempFunction = (event) => {
  console.log("keypressed " + event.target.id);
}
const keys = [
  {symbol: 'AC', id: 'clear', eventKey: 'Backspace',  type: CLEAR, width: 2}, 
  {type: "HOLDER"},
  {symbol: '=', id: 'equals', eventKey: '=', type: EQUALS, width: 2},
  {type: "HOLDER"},
  {symbol: '7', id: 'seven', eventKey: '7', type: INPUT},
  {symbol: '8', id: 'eight', eventKey: '8', type: INPUT},
  {symbol: '9', id: 'nine', eventKey: '9', type: INPUT},
  {symbol: 'X', id: 'multiply', eventKey: '*', type: OPERATION, subtype: MULTIPLY},
  {symbol: '4', id: 'four', eventKey: '4', type: INPUT},
  {symbol: '5', id: 'five', eventKey: '5', type: INPUT},
  {symbol: '6', id: 'six', eventKey: '6', type: INPUT},
  {symbol: '/', id: 'divide', eventKey: '/', type: OPERATION, subtype: DIVIDE},
  {symbol: '1', id: 'one', eventKey: '1', type: INPUT},
  {symbol: '2', id: 'two', eventKey: '2', type: INPUT},
  {symbol: '3', id: 'three', eventKey: '3', type: INPUT},
  {symbol: '-', id: 'subtract', eventKey: '-', type: OPERATION, subtype: SUBTRACT},
  {symbol: '0', id: 'zero', eventKey: '0', type: INPUT, width: 2},
  {type: 'HOLDER'},
  {symbol: '.', id: 'decimal', eventKey: '.', type: INPUT},
  {symbol: '+', id: 'add', eventKey: '+', type: OPERATION, subtype: ADD}

];


////// keypad class
class KeyPad extends React.Component {
  constructor(props){
    super(props);
    this.state ={

    }
    this.handleButton = this.handleButton.bind(this);
  }

  handleButton(event){

    let key = {
      id: event.target.id,
      eventkey: event.target.getAttribute('eventkey'),
      type: event.target.getAttribute('type'),
      subtype: event.target.getAttribute('subtype'),
      symbol: event.target.innerText
    }

    if(key.type == INPUT){
      this.props.input(key.symbol);
    }
    else if(key.type == OPERATION ){
      this.props.operation(key.eventkey);
    }else if(key.type == EQUALS){
      this.props.equals();
    }else {
      this.props.clear();
    }
  // console.log('saw button click, heres the values ' + JSON.stringify(key));
  }

  render(){

    let rows = [];
    let buttons = [];
    for(let i=0; i<5;i++){

      buttons = [];

      for(let j=0;j <4 && i*4 + j < keys.length;j++){
        let key = keys[(i*4)+j];

        let col = key.width ? "-" + key.width*3: "-3";

        if(key.type ==  "HOLDER")
          continue;

        buttons.push(
          <button className={"button col" + col + " " + BUTTON_CLASSES[key.type]}  
            id={key.id} 
            eventkey={key.eventKey} 
            onClick={this.handleButton}
            type={key.type}
            subtype={key.subtype}>
            {key.symbol}
          </button>
          );
      }

      rows.push(
        <div className="row">{buttons}</div>
      );

    }
    return(

      <div className="keyPad container">
        {rows}
      </div>

    )
  }
}

KeyPad = connect(
  (state, ownState) => {return state},
  (dispatch, ownState)=>{
    return{
      input: (integer)=> {
        dispatch({type: INPUT, integer: integer});
      },
      operation: (operation)=>{
        dispatch({type: OPERATION, operation: operation});
      },
      equals: () => {
        dispatch({type: EQUALS});
      },
      clear: () => {
        dispatch({type: CLEAR});
      }
    }
  }
)(KeyPad)
////// end keypad class

// App component section

class App extends React.Component{
  constructor(props){
    super(props)
    this.state ={
      
    }
    this.inputHandler = this.inputHandler.bind(this)

  }
  
  inputHandler(event){
 //   console.log("observed change in parent input handler. " )

  }

  componentDidMount(prevProp, prevState){
  //  console.log("componentDidMount")

  }
  render(){


    return (
        <div className={"d-flex justify-content-center  align-items-center outerDiv " + themes[this.props.theme]} >         
          <div className="innerDiv" >

            <Header title="Calculator" />

            <div className="row" >
              <div className="col"><div className="textField">{this.props.register}</div></div>
            </div>
            <div className="row" >
              <div className="col"><div className="textField" id="display">{this.props.input}</div></div>
            </div>

            <KeyPad />
          </div>
    
        </div>
    )
  }
}

App = connect(
	function mapStateToProps(state, ownProps){
    return state;
	}, 
	function mapDispatchToProps(dispatch){
		return {
			testFunction: (testInput) => {

			}
		}
	}
)(App);

// End App component

render(
	<Provider store={store}>
		<App />
	</Provider>
	, document.getElementById('root'));


              
            
!
999px

Console