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="app" />
              
            
!

CSS

              
                body{
  margin-top: 100px;
  background-color: #9CBFA7;
  display: flex; 
  align-items: center;
  justify-content: center;
}

button{
  font-size: 30px;
  border: 1px solid black;
  background-color: #616161;
  color: #F7F7F7;
}

button:hover{
  border: 1px solid white;
  color: black;
}

button:focus{
  outline: none;
}

.container{
  display: grid;
  max-width: 280px;
  border: 10px solid black;
  grid-template-columns: [first] 70px [second] 70px [third] 70px [fourth] 70px ;
  grid-template-rows: [first] 35px [second] 35px [third] 70px [fourth] 70px [five] 70px [six] 70px [seven] 70px;
}

#display{
  background-color: black;
  grid-column-start: 1;
  grid-column-end: 5;
  grid-row-start: 2;
  color: white;
  display: flex;
  align-items: flex-end;
  justify-content: flex-end;
  font-size: 30px;
  font-family: Tahoma;
}

#screen{
  background-color: black;
  grid-column-start: 1;
  grid-column-end: 5;
  grid-row-start: 1;
  color: white;
  display: flex;
  align-items: flex-end;
  justify-content: flex-end;
  color: orange;
  font-family: Tahoma;
}

#clear{
  grid-column-start: 1;
  grid-column-end: 3;
  background-color: #BF360C;
}

#divide{
  grid-column-start: 3;
  grid-row-start: 3;
  background-color: #757575;
}

#multiply{
  grid-column-start: 4;
  grid-row-start: 3;
  background-color: #757575;
}

#subtract{
  grid-column-start: 4;
  grid-row-start: 4;
  background-color: #757575;
}

#add{
  grid-column-start: 4;
  grid-row-start: 5;
  background-color: #757575;
}

#zero{
  grid-column-start: 1;
  grid-column-end: 3;
  grid-row-start: 7;
}

#equals{
  grid-row-start: 6;
  grid-row-end: span 7;
  grid-column-start: 4;
  background-color: #0D47A1;
}

#seven{
  grid-column-start: 1;
  grid-row-start: 4;
}

#nine{
  grid-column-start: 3;
  grid-row-start: 4;
}

#four{
  grid-column-start: 1;
  grid-row-start: 5;
}

#six{
  grid-column-start: 3;
  grid-row-start: 5;
}

#one{
  grid-column-start: 1;
  grid-row-start: 6;
}

#three{
  grid-column-start: 3;
  grid-row-start: 6;
}

              
            
!

JS

              
                class Input extends React.Component {
  constructor(props){
    super(props);
    this.state = {
      numbers: [{number: 9, id: 'nine'}, {number: 8, id: 'eight'}, {number: 7, id: 'seven'}, {number: 6, id: 'six'}, {number: 5, id: 'five'}, {number: 4, id: 'four'}, {number: 3, id: 'three'}, {number: 2, id: 'two'}, {number: 1, id: 'one'}, {number: 0, id: 'zero'}, {number: '.', id: 'decimal'}],
      mathOps: [{number: '+', id: 'add'}, {number: '-', id: 'subtract'}, {number: '*', id: 'multiply'}, {number: '/', id: 'divide'}],
      output: [''],
      screen: [],
      income: [''],
      equalsPressed: false
    } 
    
    this.handleChange = this.handleChange.bind(this);
    this.reset = this.reset.bind(this);
    this.splitInputIntoVariables = this.splitInputIntoVariables.bind(this);
    this.managingLogic = this.managingLogic.bind(this);
    this.together = this.together.bind(this);
    this.handlingArith = this.handlingArith.bind(this);
  }
  
  handleChange(e){
    const befoe = this.state.income;

    if(this.state.output[1] === '0'){
      if(e.target.value === '0'){
        this.setState({
          output: befoe,
          screen: (this.state.screen),
          income: (this.state.income)
        })
      }
    }

    if(e.target.value === '.' && !(this.state.output).includes('.')){
      this.setState({
        output: befoe.concat(e.target.value),
        screen: (this.state.screen).concat(e.target.value),
        income: (this.state.income).concat(e.target.value)
      })
    } else if (e.target.value !== '.' && this.state.output[1] !== '0') {
      this.setState({
        output: (befoe).concat(e.target.value),
        screen: (this.state.screen).concat(e.target.value),
        income: (this.state.income).concat(e.target.value)
      })
    }
    
    if(this.state.equalsPressed === true){
      this.setState({
         output: [''].concat(e.target.value),
         screen: [''].concat(e.target.value),
         income: [''].concat(e.target.value),
         equalsPressed: false
      })
    }
    
  }
  
  handlingArith(e){
    const before = this.state.screen;
    
    if(this.state.equalsPressed){
      this.setState({
        equalsPressed: false
      })
    }

    if(isNaN(Number(before[before.length - 1]))){
      //if the last element in the screen is an operator and the target is an operator
      if(isNaN(Number(before[before.length - 1])) && e.target.value === '-'){ // if the last element on the screen is an operator and the current operator is '-' add it normally
        this.setState({
          screen: before.concat(e.target.value),
          output: [''].concat(e.target.value),
          income: ['']
        })
      } else if (before[before.length - 1] === '-' && isNaN(Number(e.target.value))) {
        this.setState({
          screen: before.slice(0, before.length - 2).concat(e.target.value),
          output: [''].concat(e.target.value),
          income: ['']
        })
      }
      
    } else{
      this.setState({
        screen: before.concat(e.target.value),
        output: [''].concat(e.target.value),
        income: ['']
      })
    }
  }
  
  reset(){
    this.setState({
      output: [''],
      screen: [],
      income: ['']
    })
  }
  
  splitInputIntoVariables(input){
    let changingInput = input.split("");
    if(changingInput[0] === ''){
      changingInput.shift()
    }
    let arrayWithNewInputs = [];
    
    var numbers = [];
    for(let i = 0; i < changingInput.length; i++){
      if(Number(changingInput[i]) || changingInput[i] === '0'){
         numbers.push(changingInput[i])
      } else {
         if(changingInput[i] === '.'){
            numbers.push(changingInput[i])
         } else if(changingInput[i] === '-' && typeof arrayWithNewInputs[arrayWithNewInputs.length - 1] === 'string' && numbers.length === 0) {
           numbers.push(changingInput[i])
         } else {
            arrayWithNewInputs.push(Number(numbers.join("")));
            numbers = [];
            arrayWithNewInputs.push(changingInput[i])
         }
      }
    }
    
    if(numbers.length > 0){
      arrayWithNewInputs.push(Number(numbers.join("")));
    }
    
    return arrayWithNewInputs;
  }
  
  managingLogic(arr){
    var results = arr.shift();
    while(arr.length > 0){
      switch(arr[0]){
        case '+':
          results += arr[1];
          arr.splice(0, 2);
          break;
        case '-':
          results -= arr[1];
          arr.splice(0, 2);
          break;
        case '*':
          results *= arr[1];
          arr.splice(0, 2);
          break;
        case '/':
          results /= arr[1];
          arr.splice(0, 2);
          break;
        default: 
          results += 0;
          break;
      }
    }
    
    return results;
  }
  
  together(){
    var vary = this.splitInputIntoVariables(this.state.screen.join(""));
    var calc = this.managingLogic(vary);
    this.setState({
      output: ['', calc.toString()],
      equalsPressed: true
    })
  }
  
  render(){
    let numberButtons = this.state.numbers.map(item => {
      return(
        <button id={item.id} key={item.id} value={item.number} onClick={this.handleChange}>{item.number}</button>
      )
    })
    let mathButtons = this.state.mathOps.map(item => {
      return(
        <button id={item.id} key={item.id} onClick={this.handlingArith} value={item.number}>{item.number}</button>
      )
    })
    
    return(
      <div className="container">
          <button id='clear' key='clear' value='AC' onClick={this.reset}>AC</button>
          {numberButtons}
          {mathButtons}
          <button id="equals" key="equals" value="=" onClick={this.together}>=</button>
          <div id="screen" key="screen">{this.state.screen.join("")}</div>
          <div id="display" key="display">{this.state.output.length > 1 ? this.state.output.join("") : '0'}</div>
      </div>
      
    )
  }
}
ReactDOM.render(<Input />, document.getElementById('app'))
              
            
!
999px

Console