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

              
                <script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>


<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

#app


<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
              
            
!

CSS

              
                html,body {
  height: 100vh;
    background-image: url(http://3.bp.blogspot.com/-f8Rq51u1rCw/UAuUyVMIaTI/AAAAAAAAA4g/gLbT0LxxozM/s1600/Medium+Barre+or+gray+granite.jpg);
}

* {
  margin: 0;
  padding: 0;
}

#main-container {
  padding-top: 40px;
}

#calc {
  background: black;
  width: 40%;
  min-width: 300px;
  height: 350px;
  border: solid 3px black;
  margin: auto;
}

#displayScreen {
  background: black;
  width: 100%;
  height: 70px;
  float: right;
}

#display {
  color: orange;
  text-align: right;
  padding-right: 10px;
  font-size: 50px;
}

.row.no-gutter {
  margin-left: 0;
  margin-right: 0;
}

.row.no-gutter [class*='col-']:not(:first-child),
.row.no-gutter [class*='col-']:not(:last-child) {
  padding-right: 0;
  padding-left: 0;
}

#ACButton {
  height: 50px;
  background: #47d147;
  border-right: solid 2px black;
  border-bottom: solid 2px black;
  text-align: center;
    padding-top: 10px;
}

#Button1 {
  height: 50px;
  background: #f2f2f2;
  border-bottom: solid 2px black;
  border-right: solid 2px black;
  text-align: center;
    padding-top: 10px;
}

#button0 {
  height: 50px;
  background: #f2f2f2;
  border-bottom: solid 2px black;
  border-right: solid 2px black;
  text-align: center;
    padding-top: 10px;
}

#Button2 {
  height: 50px;
  background: #f2f2f2;
  border-bottom: solid 2px black;
  border-right: solid 2px black;
  text-align: center;
    padding-top: 10px;
}

#Button3 {
  height: 50px;
  background: #f2f2f2;
  border-bottom: solid 2px black;
  border-right: solid 2px black;
  text-align: center;
    padding-top: 10px;
}

#Button4 {
  height: 50px;
  background: #f2f2f2;
  border-bottom: solid 2px black;
  border-right: solid 2px black;
  text-align: center;
    padding-top: 10px;
}

#Button5 {
  height: 50px;
  background: #f2f2f2;
  border-bottom: solid 2px black;
  border-right: solid 2px black;
  text-align: center;
    padding-top: 10px;
}

#Button6 {
  height: 50px;
  background: #f2f2f2;
  border-bottom: solid 2px black;
  border-right: solid 2px black;
  text-align: center;
    padding-top: 10px;
}

#Button7 {
  height: 50px;
  background: #f2f2f2;
  border-bottom: solid 2px black;
  border-right: solid 2px black;
  text-align: center;
    padding-top: 10px;
}

#Button8 {
  height: 50px;
  background: #f2f2f2;
  border-bottom: solid 2px black;
  border-right: solid 2px black;
  text-align: center;
    padding-top: 10px;
}

#Button9 {
  height: 50px;
  background: #f2f2f2;
  border-bottom: solid 2px black;
  border-right: solid 2px black;
  text-align: center;
    padding-top: 10px;
}

#DivideButton {
  height: 50px;
  background: #66ccff;
  border-bottom: solid 2px black;
  border-right: solid 2px black;
  text-align: center;
    padding-top: 10px;
}

#MinusButton {
  height: 50px;
  background: #66ccff;
  border-bottom: solid 2px black;
  text-align: center;
    padding-top: 10px;
}

#PlusButton {
  height: 50px;
  background: #66ccff;
  border-bottom: solid 2px black;
  text-align: center;
    padding-top: 10px;
}

#TimesButton {
  height: 50px;
  background: #66ccff;
  border-bottom: solid 2px black;
  text-align: center;
    padding-top: 10px;
}

#EqualsButton {
  height: 100px;
  background: #cc33ff;
  border-bottom: solid 2px black;
  text-align: center;
  padding-top: 40px;
}

#DecimalButton {
  height: 50px;
  background: #f2f2f2;
  border-bottom: solid 2px black;
  border-right: solid 2px black;
  text-align: center;
  padding-top: 10px;
}

              
            
!

JS

              
                let i = 0;

class Calculator extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      display: 0,
      input: '',
      numArr: [],
    }
    this.handleClick = this.handleClick.bind(this);
    this.doMath = this.doMath.bind(this);
  }
  doMath(numArr) {
    console.log('DO MATH')
    console.log(numArr)
    
    // CHECK FOR DOUBLE OPERATORS
    
    
      // LOOP THROUGH THE ARRAY
  while (numArr.length > 1) {  
    
    console.log(numArr)
    
      // MULTIPLY FUNCTION
      if (numArr[i] === 'x') {
        let mult = (numArr[i-1] * numArr[i+1])
        console.log("Mult Result: " + mult)
        numArr.splice([i-1], 3, mult)
        console.log("updatedArr: " + numArr)
        i = 0;
      }
      
      // DIVIDE FUNCTION
      if (numArr[i] === '/' && !numArr.includes('x')) {
        let div = (numArr[i-1] / numArr[i+1])
        console.log("Div Result: " + div)
                numArr.splice([i-1], 3, div)
        console.log("updatedArr: " + numArr)
        i = 0;
      }
    
      if (numArr[i] === '-' && !numArr.includes('x') && !numArr.includes('/') && numArr.includes('+')) {
        let res = (numArr[i-1] - numArr[i+1])
        console.log("Sub Result: " + res)
        numArr.splice([i-1], 3, res)
        console.log("updatedArr: " + numArr)
        i = 0;
      }
    
    
      if (numArr[i] === '+' && !numArr.includes('x') && !numArr.includes('/')) {
        let sum = (numArr[i-1] + numArr[i+1])
        console.log("Add Result: " + sum)
        numArr.splice([i-1], 3, sum)
        console.log("updatedArr: " + numArr)
        i = 0;
      }
    
          if (numArr[i] === '-' && !numArr.includes('x') && !numArr.includes('/') && !numArr.includes('+')) {
        let res = (numArr[i-1] - numArr[i+1])
        console.log("Sub Result: " + res)
        numArr.splice([i-1], 3, res)
        console.log("updatedArr: " + numArr)
        i = 0;
      }
    

    
      // RESULT
      if (numArr.length === 1) {
        console.log("ANSWER: " + numArr)
      }
      else if (numArr.length === 2) {
        console.log("OH NOES!: " + numArr)
        numArr.splice(0, 1)
      }
    i++;
  }
    console.log("RESULT: " + numArr)
   }
  handleClick(event) {
    if (event.target.id === 'one') {
      let oneVal = '1'
      this.setState({
        display: this.state.input += oneVal
      })
    }
    else if (event.target.id === 'two') {
      let twoVal = '2'
      this.setState({
        display: this.state.input += twoVal
      })
    }
    else if (event.target.id === 'three') {
      let threeVal = '3'
      this.setState({
        display: this.state.input += threeVal
      })
    }
    else if (event.target.id === 'four') {
      let fourVal = '4'
      this.setState({
        display: this.state.input += fourVal
      })
    }
    else if (event.target.id === 'five') {
      let fiveVal = '5'
      this.setState({
        display: this.state.input += fiveVal
      })
    }
    else if (event.target.id === 'six') {
      let sixVal = '6'
      this.setState({
        display: this.state.input += sixVal
      })
    }
    else if (event.target.id === 'seven') {
      let sevenVal = '7'
      this.setState({
        display: this.state.input += sevenVal
      })
    }
    else if (event.target.id === 'eight') {
      let eightVal = '8'
      this.setState({
        display: this.state.input += eightVal
      })
    }
    else if (event.target.id === 'nine') {
      let nineVal = '9'
      this.setState({
        display: this.state.input += nineVal
      })
    }
    else if (event.target.id === 'zero') {
      let zeroVal = '0'
      if (this.state.input[0] !== '0') {
        this.setState({
        display: this.state.input += zeroVal
      })
      }
      console.log(this.state.input)
      console.log(this.state.input[0])
    }
    else if (event.target.id === "decimal") {
      let decVal = '.'
      console.log(this.state.input)
      if (!this.state.input.includes('.')) {
        this.setState({
          display: this.state.input += decVal
        })
      }

    }
    else if (event.target.id === "add") {      
      let parsedNum = parseFloat(this.state.input)
      
      if (this.state.input === '') {
        this.state.numArr.splice(this.state.numArr.length-1, 1, "+")
        this.setState({
        numArr: this.state.numArr,
        display: '+'
      })
      }
      else {
      this.state.numArr.push(parsedNum)
      console.log("Num Arr: " + this.state.numArr)
      this.state.numArr.push('+')
      console.log("Num Arr: " + this.state.numArr)  
      this.setState({
        display: '+',
        input: ''
      })
      }
    }
    else if (event.target.id === "subtract") {
      let parsedNum = parseFloat(this.state.input)
      
      if (this.state.input === '') {
        console.log("buhh")
        console.log(this.state.numArr)
        console.log(this.state.numArr.length)
        this.state.numArr.splice(this.state.numArr.length-1, 1, "-")
        this.setState ({
        numArr: this.state.numArr,
        display: '-'
        })
      }
      else {
      this.state.numArr.push(parsedNum)
      console.log("Num Arr: " + this.state.numArr)
      this.state.numArr.push('-')
      this.setState({
        display: '-',
        input: ''
      })
      }
    }
     else if (event.target.id === "divide") {
      let parsedNum = parseFloat(this.state.input)
      
      if (this.state.input === '') {
      this.state.numArr.splice(this.state.numArr.length-1, 1, "/")
      this.setState({  
        numArr: this.state.numArr, 
        display: '/'
      })
      }
      else {
      this.state.numArr.push(parsedNum)
      console.log("Num Arr: " + this.state.numArr)
      this.state.numArr.push('/')
      this.setState({
        display: "/",
        input: ''
      })
      }
    }
    else if (event.target.id === "multiply") {
      let parsedNum = parseFloat(this.state.input)
      
      if (this.state.input === '') {
        this.state.numArr.splice(this.state.numArr.length-1, 1, "x")
      this.setState({
        numArr: this.state.numArr,
        display: 'x'
      })
      }
      else {
      this.state.numArr.push(parsedNum)
      console.log("Num Arr: " + this.state.numArr)
      this.state.numArr.push('x')
      this.setState({
        display: 'x',
        input: ''
      })
      }
    }
    else if (event.target.id === "clear") {
      this.setState({
        display: 0,
        input: '',
        numArr: []
      })
    }
    else if (event.target.id === "equals") {
      let parsedNum = parseFloat(this.state.input)
      this.state.numArr.push(parsedNum)

      console.log("Num Arr: " + this.state.numArr)
      this.doMath(this.state.numArr);
      this.setState({
        input: ''
      })
      // how do i return the result?
      let fin = this.state.numArr[0]
      console.log("fin length: " + fin.length)
      // fin.toFixed(5)
      
      this.setState({
        display: this.state.numArr[0],
        input: fin
      })
    }
  }
  render() {
    return (
    <div>
      <div id="main-container" class="container">
       <div id="calc">
      <div class="row">
      <div class="col-md-12">  
      {/* DISPLAY */}
        <Display display={this.state.display}/>
       </div>
       </div>
          
        {/* NUMBER BUTTONS */}      
      <div class="row no-gutter">
        <div class="col-md-6">
          <ACButton handleClick={this.handleClick}/>
        </div>
        <div class="col-md-3">
          <DivideButton handleClick={this.handleClick}/>
        </div>
        <div class="col-md-3">
          <TimesButton handleClick={this.handleClick}/>
        </div>
      </div>
      <div class="row no-gutter">
          <div class="col-md-3">
            <Button7 handleClick={this.handleClick}/>
          </div>
          <div class="col-md-3">
            <Button8 handleClick={this.handleClick}/>
          </div>
          <div class="col-md-3">
            <Button9 handleClick={this.handleClick}/> 
          </div>
          <div class="col-md-3">
            <MinusButton handleClick={this.handleClick}/>
          </div>
        </div>
      <div class="row no-gutter">
        <div class="col-md-3">
          <Button4 handleClick={this.handleClick}/>
        </div>
        <div class="col-md-3">
        <Button5 handleClick={this.handleClick}/>
        </div>
        <div class="col-md-3">
        <Button6 handleClick={this.handleClick}/>   
        </div>
        <div class="col-md-3">
        <PlusButton handleClick={this.handleClick}/>
        </div>
        </div>
 <div class="row no-gutter">
    <div class="col-md-9">
       <div class="row no-gutter">
         <div class="col-md-4">
          <Button1 handleClick={this.handleClick}/>
        </div>
        <div class="col-md-4">
        <Button2 handleClick={this.handleClick}/>
        </div>
        <div class="col-md-4">
        <Button3 handleClick={this.handleClick}/>   
        </div>
       </div>
      <div class="row no-gutter">
        <div class="col-md-8">
           <Button0 handleClick={this.handleClick}/>
        </div>
        <div class="col-md-4">
          <DecimalButton handleClick={this.handleClick}/>
        </div>
      </div>
    </div>
    <div class="col-md-3">
      <EqualsButton handleClick={this.handleClick}/>
    </div>
</div>
               
        </div>
      </div>
    </div>
    )
  }
}

class Display extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      display: 0
    }
  }
  render() {
    return (
    <div>
      <div id="displayScreen">
        <div id="display">{this.props.display}</div>
      </div>
    </div>
    )
  }
}

class Button0 extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      id: '0',
      value: 0
    }
  }
  render() {
    return (
      <div>
    <div id="button0" onClick={this.props.handleClick}><p id="zero">{this.state.id}</p></div>
      </div>
    )
  }
}

class Button1 extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      id: '1',
      value: 1
    }
  }
  render() {
    return (
    <div id="Button1" onClick={this.props.handleClick}><p id="one">{this.state.id}</p></div>
    )
  }
}

class Button2 extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      id: '2',
      value: 2
    }
  }
  render() {
    return (
    <div id="Button2" onClick={this.props.handleClick}><p id="two">{this.state.id}</p></div>
    )
  }
}

class Button3 extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      id: '3',
      value: 3
    }
  }
  render() {
    return (
    <div id="Button3" onClick={this.props.handleClick}><p id="three">{this.state.id}</p></div>
    )
  }
}

class Button4 extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      id: '4',
      value: 4
    }
  }
  render() {
    return (
    <div id="Button4" onClick={this.props.handleClick}><p id="four">{this.state.id}</p></div>
    )
  }
}

class Button5 extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      id: '5',
      value: 5
    }
  }
  render() {
    return (
    <div id="Button5" onClick={this.props.handleClick}><p id="five">{this.state.id}</p></div>
    )
  }
}

class Button6 extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      id: '6',
      value: 6
    }
  }
  render() {
    return (
    <div id="Button6" onClick={this.props.handleClick}><p id="six">{this.state.id}</p></div>
    )
  }
}

class Button7 extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      id: '7',
      value: 7
    }
  }
  render() {
    return (
    <div id="Button7" onClick={this.props.handleClick}><p id="seven">{this.state.id}</p></div>
    )
  }
}

class Button8 extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      id: '8',
      value: 8
    }
  }
  render() {
    return (
    <div id="Button8" onClick={this.props.handleClick}><p id="eight">{this.state.id}</p></div>
    )
  }
}

class Button9 extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      id: '9',
      value: 9
    }
  }
  render() {
    return (
    <div id="Button9" onClick={this.props.handleClick}><p id="nine">{this.state.id}</p></div>
    )
  }
}

class DecimalButton extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      id: '.',
      value: '.'
    }
  }
  render() {
    return (
    <div id="DecimalButton" onClick={this.props.handleClick}><p id="decimal">{this.state.id}</p></div>
    )
  }
}

class ACButton extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      id: 'AC',
      value: '.'
    }
  }
  render() {
    return (
    <div id="ACButton" onClick={this.props.handleClick}><p id="clear">{this.state.id}</p></div>
    )
  }
}

class PlusButton extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      id: '+',
      value: '+'
    }
  }
  render() {
    return (
    <div id="PlusButton" onClick={this.props.handleClick}><p id="add">{this.state.id}</p></div>
    )
  }
}

class MinusButton extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      id: '-',
      value: '-'
    }
  }
  render() {
    return (
    <div id="MinusButton" onClick={this.props.handleClick}><p id="subtract">{this.state.id}</p></div>
    )
  }
}

class TimesButton extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      id: 'x',
      value: 'x'
    }
  }
  render() {
    return (
    <div id="TimesButton" onClick={this.props.handleClick}><p id="multiply">{this.state.id}</p></div>
    )
  }
}

class DivideButton extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      id: '/',
      value: '/'
    }
  }
  render() {
    return (
    <div id="DivideButton" onClick={this.props.handleClick}><p id="divide">{this.state.id}</p></div>
    )
  }
}

class EqualsButton extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      id: '=',
      value: '='
    }
  }
  render() {
    return (
    <div id="EqualsButton" onClick={this.props.handleClick}><p id="equals">{this.state.id}</p></div>
    )
  }
}


ReactDOM.render(<Calculator />, document.getElementById('app'));
              
            
!
999px

Console