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 href="https://fonts.googleapis.com/css2?family=VT323&display=swap" rel="stylesheet">

<div id='root'></div>
<div class='test'>
</div>
              
            
!

CSS

              
                html,
body,
#app {
  min-width: 625px;
  min-height: 400px;
}

html {
  background: #fab1a0;
}

#app {
  position: relative;
  top: 50px;
  display: flex;
  flex-wrap: wrap;
  /*   border: 3px solid green; */
  max-width: 725px;
  margin: auto;
}

.clickable {
  cursor: pointer;
  height: 10vh;
  border: 2px solid black;
  margin: 5px;
  text-align: center;
  /*   background: #ffeaa7; */
  background: #f5cd79;
  border-radius: 5px;
  box-shadow: 1px 1px 1px black;
}

#display {
  height: 12vh;
  min-height: 63px;
  width: 100%;
  border: 2px solid black;
  position: relative;
  background: #74b9ff;
  /*   color: white; */
  color: white;
  text-shadow: 2px 2px black;
  font-size: 50px;
  text-align: end;
  padding-right: 20px;
  font-family: "VT323", monospace;
}

.test {
  font-weight: bolder;
  position: relative;
  top: 50px;
}

.numButtons {
  border: 5px solid black;
  background: #00cec9;
  height: 57vh;
  width: 50vw;
  min-height: 323px;
  max-width: 265px;
  border-radius: 5px;
  min-width: 280px;
  margin: auto;
  display: grid;
  padding: 15px 5px 0px 5px;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  grid-template-areas:
    "result result result result"
    "one two three four"
    "five six seven eight"
    "nine zero eql eql";
}

#zero {
  grid-area: zero;
}

#clear {
  grid-area: clear;
  background: #e74c3c;
}

#display {
  grid-area: result;
}

.opButtons {
  background: #485460;
  display: grid;
  width: 25vw;
  height: 40vh;
  min-height: 229px;
  position: relative;
  top: 77px;
  right: 10%;
  padding: 10px 5px 5px 5px;
  border-radius: 5px;
  min-width: 145px;
  max-width: 202px;
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(3, 1fr);
  border: 2px solid black;
  grid-template-areas:
    "add sub"
    "times div"
    "dec clear";
}

.op {
  background: #f8a5c2;
  min-height: 53px;
}

.num {
  min-height: 53px;
}

#equals {
  grid-area: eql;
  background: #2ecc71;
  min-height: 53px;
}

              
            
!

JS

              
                class App extends React.Component {
  constructor() {
    super();

    this.clear = this.clear.bind(this);
    this.addNum = this.addNum.bind(this);
    this.addition = this.addition.bind(this);
    this.firstNum = this.firstNum.bind(this);
    this.secondNum = this.secondNum.bind(this);
    this.equals = this.equals.bind(this);
    this.subtraction = this.subtraction.bind(this);
    this.times = this.times.bind(this);
    this.divide = this.divide.bind(this);
    this.checkZero = this.checkZero.bind(this);
    this.setDecimal = this.setDecimal.bind(this);
    this.handleOpClicks = this.handleOpClicks.bind(this);

    this.state = {
      display: [0],
      firstNum: "",
      secNum: [],
      operator: "",
      onFirstNum: true,
      decimal: false,
      opClicks: 0,
      negative: true
    };
  }

  clear() {
    $("#decimal").prop("disabled", false);
    $("#zero").prop("disabled", false);
    $(".num").prop("disabled", false);

    this.setState({
      display: [0],
      onFirstNum: true,
      firstNum: "",
      secNum: "",
      operator: "",
      opClicks: 0,
      negative: true
    });
  }

  checkZero() {
    let button = $(event.target).text();
    if (
      this.state.display[0] == 0 &&
      this.state.display.length <= 1 &&
      button == "0"
    ) {
      $("#zero").prop("disabled", true);
    } else if (
      this.state.secNum == "" &&
      this.state.firstNum != "" &&
      button == "0"
    ) {
      $("#zero").prop("disabled", true);
    } else {
      $("#zero").prop("disabled", false);
    }
  }

  setDecimal() {
    let decimal = $(event.target).text();
    if (decimal == ".") {
      $("#decimal").prop("disabled", true);
    }
  }

  addNum() {
    this.setDecimal();
    this.checkZero();
    if (this.state.display.length > 11 && this.state.onFirstNum == true) {
      $(".num").prop("disabled", true);
    } else if (
      this.state.display.length == 13 &&
      this.state.onFirstNum == false
    ) {
      $(".num").prop("disabled", false);
    } else if (
      this.state.display.length > 11 &&
      this.state.onFirstNum == false
    ) {
      $(".num").prop("disabled", true);
    }

    if (this.state.onFirstNum == true) {
      this.firstNum();
    } else {
      this.secondNum();
    }
  }

  secondNum() {
    if (this.state.display == this.state.firstNum) {
      let num = $(event.target).text();
      this.setState({
        display: [num],
        secNum: [num]
      });
    } else {
      let num = $(event.target).text();
      this.setState({
        display: [...this.state.display, num],
        secNum: [...this.state.display, num]
      });
    }
  }

  firstNum() {
    if (this.state.display[0] === 0) {
      let num = $(event.target).text();
      this.setState({
        display: [num],
        negative: false
      });
    } else {
      let num = $(event.target).text();
      this.setState({
        display: [...this.state.display, num],
        negative: false
      });
    }
  }

  addition() {
    $("#decimal").prop("disabled", false);
    $(".num").prop("disabled", false);

    if (this.state.display == "-") {
      this.setState({
        operator: "+",
        display: ""
      });
    } else {
      this.setState({
        firstNum: this.state.display,
        operator: "+",
        onFirstNum: false,
        opClicks: this.state.opClicks + 1,
        negative: true
      });
    }
    this.handleOpClicks();
  }

  subtraction() {
    $("#decimal").prop("disabled", false);
    $(".num").prop("disabled", false);
    if (
      this.state.negative == true &&
      this.state.firstNum == "" &&
      this.state.secNum == "" &&
      this.state.operator == ""
    ) {
      this.setState({
        display: "-",
        negative: false
      });
    } else if (
      this.state.negative == false &&
      this.state.firstNum == "" &&
      this.state.secNum == "" &&
      this.state.operator == ""
    ) {
      this.setState({
        firstNum: this.state.display,
        operator: "-",
        onFirstNum: false,
        negative: true,
        opClicks: this.state.opClicks + 1
      });
    } else if (
      this.state.negative == true &&
      this.state.firstNum != "" &&
      this.state.secNum == "" &&
      this.state.operator != ""
    ) {
      this.setState({
        display: "-",
        negative: false
      });
    } else if (
      this.state.negative == false &&
      this.state.firstNum != "" &&
      this.state.secNum == "" &&
      this.state.operator != ""
    ) {
      this.setState({
        operator: "-",
        negative: true,
        opClicks: this.state.opClicks + 1
      });
    } else if (this.secNum != "") {
      this.setState({
        operator: "-",
        negative: true,
        opClicks: this.state.opClicks + 1
      });
    }
    this.handleOpClicks();
  }

  times() {
    $("#decimal").prop("disabled", false);
    $(".num").prop("disabled", false);

    this.setState({
      firstNum: this.state.display,
      operator: "*",
      onFirstNum: false,
      opClicks: this.state.opClicks + 1,
      negative: true
    });
    this.handleOpClicks();
  }

  divide() {
    $("#decimal").prop("disabled", false);
    $(".num").prop("disabled", false);

    this.setState({
      firstNum: this.state.display,
      operator: "/",
      onFirstNum: false,
      opClicks: this.state.opClicks + 1,
      negative: true
    });
    this.handleOpClicks();
  }

  handleOpClicks() {
    if (this.state.opClicks == 1 && this.state.secNum != "") {
      this.equals();
    }
  }

  equals() {
    let firstNum;
    let result;
    $(".num").prop("disabled", false);
    $("#decimal").prop("disabled", false);
    let operator = this.state.operator;
    if (typeof this.state.firstNum == "object") {
      firstNum = this.state.firstNum.join("");
    } else if (typeof this.state.firstNum == "number") {
      firstNum = this.state.firstNum;
    }

    let result1 = Number(firstNum);
    let Num2 = this.state.secNum.join("");
    let result2 = Number(Num2);

    switch (operator) {
      case "+":
        result = result1 + result2;
        break;
      case "-":
        result = result1 - result2;
        break;
      case "*":
        result = result1 * result2;
        break;
      case "/":
        result = result1 / result2;
    }
    let product;
    let strResult = String(result);
    if (strResult.length > 13) {
      let resArray = strResult.split("");
      let trimArr = resArray.slice(0, 13);
      product = trimArr;
    } else {
      product = result;
    }

    this.setState({
      display: product,
      firstNum: product,
      secNum: "",
      opClicks: 1,
      negative: false
    });
  }

  render() {
    return (
      <div>
        <Calculator
          display={this.state.display}
          clear={this.clear}
          addNum={this.addNum}
          addition={this.addition}
          equals={this.equals}
          subtraction={this.subtraction}
          times={this.times}
          divide={this.divide}
        />
      </div>
    );
  }
}

class Calculator extends React.Component {
  render() {
    return (
      <div id="app">
        <div class="numButtons">
          <button id="display">{this.props.display}</button>
          <button id="zero" class="clickable num" onClick={this.props.addNum}>
            0
          </button>
          <button id="one" class="clickable num" onClick={this.props.addNum}>
            1
          </button>
          <button id="two" class="clickable num" onClick={this.props.addNum}>
            2
          </button>
          <button id="three" class="clickable num" onClick={this.props.addNum}>
            3
          </button>
          <button id="four" class="clickable num" onClick={this.props.addNum}>
            4
          </button>
          <button id="five" class="clickable num" onClick={this.props.addNum}>
            5
          </button>
          <button id="six" class="clickable num" onClick={this.props.addNum}>
            6
          </button>
          <button id="seven" class="clickable num" onClick={this.props.addNum}>
            7
          </button>
          <button id="eight" class="clickable num" onClick={this.props.addNum}>
            8
          </button>
          <button id="nine" class="clickable num" onClick={this.props.addNum}>
            9
          </button>
          <button id="equals" class="clickable" onClick={this.props.equals}>
            =
          </button>
        </div>
        <div class="opButtons">
          <button id="add" class="clickable op" onClick={this.props.addition}>
            +
          </button>
          <button id="clear" class="clickable op" onClick={this.props.clear}>
            Clear
          </button>
          <button
            id="subtract"
            class="clickable op"
            onClick={this.props.subtraction}
          >
            -
          </button>

          <button id="multiply" class="clickable op" onClick={this.props.times}>
            *
          </button>
          <button id="divide" class="clickable op" onClick={this.props.divide}>
            /
          </button>
          <button id="decimal" class="clickable op" onClick={this.props.addNum}>
            .
          </button>
        </div>
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById("root"));

//13
//ae([a,i,l,a,c]),o.assert.strictEqual(oe(document.getElementById("display")),"-25",'The sequence "5 * - 5" = should produce an output of "-25"  GOOD

//'),f(),ae([a,i,l,"add",a,c]),o.assert.strictEqual(oe(document.getElementById("display")),"10",'The sequence "5 * - + 5" = should produce an output of "10"

// '),f(),ae([a,"add","add",a,c]),o.assert.strictEqual(oe(document.getElementById("display")),"10",'The sequence "5 + + 5" = should produce an output of "10" ')

//ae([a,l,t,c,u,t,c]),o.assert.strictEqual(oe(document.getElementById("display")),"1.5",'The sequence "5 - 2 = / 2 =" should produce an output of "1.5"  GOOD

//ae([a,l,t,c,u,t,c]),o.assert.strictEqual(oe(document.getElementById("display")),"1.5",'The sequence "5 - 2 = / 2 =" should produce an output of "1.5"   GOOD

//'),f(),ae([a,"add",a,c,"add",r,c]),o.assert.strictEqual(oe(document.getElementById("display")),"13",'The sequence "5 + 3 = + 3 =" should produce an output of "13" ')

// '),f(),ae([a,"add",a,c,"add",r,c]),o.assert.strictEqual(oe(document.getElementById("display")),"13",'The sequence "5 + 3 = + 3 =" should produce an output of "13" ')

              
            
!
999px

Console