123

Pen Settings

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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <body>
  <container class="calc-body">
    <h3 class="calc-title">CALCULATOR</h3>
    <div class="calc-display">
      <h1 class="calc-nums">0</h1>
      <h5 class="calc-equasion">0</h5>
    </div>
    <div class="calc-buttons">
      <section class="calc-col">
        <div class="calc-row">
          <button class="button" id="AC" data-name="AC">AC</button>
          <button class="button" id="CE" data-name="CE">CE</button>
          <button class="button" id="divide" data-name="/">&divide</button>
        </div>
        <div class="calc-row">
          <button class="button" id="seven" data-name="7">7</button>
          <button class="button" id="eight" data-name="8">8</button>
          <button class="button" id="nine" data-name="9">9</button>
        </div>
        <div class="calc-row">
          <button class="button" id="four" data-name="4">4</button>
          <button class="button" id="five" data-name="5">5</button>
          <button class="button" id="six" data-name="6">6</button>
        </div>
        <div class="calc-row">
          <button class="button" id="one" data-name="1">1</button>
          <button class="button" id="two" data-name="2">2</button>
          <button class="button" id="three" data-name="3">3</button>
        </div>
        <div class="calc-row">
          <button class="button" id="zero" data-name="0">0</button>
          <button class="button" id="dot" data-name=".">.</button>
        </div>
      </section>
      <section class="calc-col-2">
        <div class="calc-row">
          <button class="button" id="multiply" data-name="*">&times</button>
        </div>
        <div class="calc-row">
          <button class="button" id="subtract" data-name="-">-</button>
        </div>
        <div class="calc-row">
          <button class="button" id="add" data-name="+">+</button>
        </div>
        <div class="calc-row">
          <button class="button" id="equals" data-name="=">=</button>
        </div>
      </section>
    </div>
  </container>
</body>
            
          
!
            
              body {
  display: flex;
  background-color: #909097;
  font-family: 'Orbitron', sans-serif;
}

.calc-body {
  text-align: center;
  background-color: #22212f;
  color: #fff;
  border-radius: 15px;
  width: 325px;
  height: 500px;
  box-shadow: 1px 1px 50px #000;
  margin: 100px auto;
  border-right: 10px solid rgba(0, 0, 0, .1);
}

.calc-title {
  font-weight: 200;
}

.calc-display {
  text-align: right;
  background-color: #56565A;
  width: 85%;
  height: 75px;
  border: 1px solid #4d4d51;
  border-radius: 15px;
  margin: 15px auto;
}

.calc-nums {
  position: relative;
  top: -15px;
  left: -10px;
}

.calc-equasion {
  position: relative;
  top: -38px;
  left: -12px;
}

.calc-buttons {
  display: flex;
  justify-content: center;
  margin: auto;
}

.button {
  position: relative;
  background-color: #2b2b2d;
  font-family: 'Orbitron', sans-serif;
  font-size: 16px;
  border: none;
  border-bottom: 2px solid #555556;
  border-right: 1px solid #555556;
  border-radius: 15px;
  color: #fff;
  width: 55px;
  height: 50px;
  box-shadow: 1px 2px 2px #000;
  margin: 5px 5px;
  &:active{
    top: 4px;
  }
  &:focus {
    outline: 0;
  }
}

.calc-col-1 {
  display: flex;
}

.calc-row {
  display: flex;

}

.calc-col-2 {
  display: flex;
  flex-direction: column;
}

#AC,
#CE {
  background-color: #491e22;
}

#equals {
  height: 112px;
}

#zero {
  width: 122px;
}
            
          
!
            
              const Calculator = (function() {
  let numsDisplay = $(".calc-nums");
  let eqDisplay = $(".calc-equasion");


  let _model = {
    num: "",
    nums: [],
    equasion: [],
    numCount: 0,
    eqCount: 0,
    //checks if parameter is a number
    isNumber: function(buttonName) {
      return /\d/.test(parseInt(buttonName));
    },
    //checks if parameter is a clear button
    isClear: function(buttonName) {
      return /((AC)|(CE))/g.test(buttonName);
    },
    //checks if parameter is a math operator
    isOperator: function(buttonName) {
      return /\+|-|\*|\//.test(buttonName);
    }
  };

  let _view = {
    printNums: function(nums) {
      numsDisplay.text(nums);
    },
    printEq: function(numsArray) {
      eqDisplay.text(numsArray.join(""));
    },
    printError: function() {
      numsDisplay.text("Error");
      eqDisplay.text("");
    }

  };

  let _controller = {

    //figures out which operators should happen when different buttons are pressed
    findType: function(buttonName) {

      if (_model.isNumber(buttonName) || buttonName === ".") {
        _model.num += buttonName;
        _view.printNums(_model.num);
        _view.printEq(_model.equasion);
        _model.numCount++;
        _model.eqCount++;
        $("#divide, #multiply, #add, #subtract").prop("disabled", false);
      } else if (_model.isClear(buttonName)) {
        this.clearNums(buttonName);
      } else if (_model.isOperator(buttonName)) {
        _model.nums.push(_model.num);
        _model.equasion.push(_model.num);
        _model.num = "";
        _model.numCount = 0;
        _model.nums.push(buttonName);
        _model.equasion.push(buttonName);
        _view.printNums(buttonName);
        _view.printEq(_model.equasion);
        _model.numCount++;
        _model.eqCount++;
        $("#divide, #multiply, #add, #subtract").prop("disabled", true); //stops user from hitting two operators in a row.
      } else if (buttonName === "=") {
        _model.nums.push(_model.num);
        _model.equasion.push(_model.num);
        this.calculateTotal(_model.equasion);
      }
      this.checkForError();

    },
    //clears calculator
    clearNums: function(buttonName) {
      if (buttonName === "AC") {
        _model.equasion = [];
        _model.eqCount = 0;
        eqDisplay.text("0");
      }
      _model.nums = [];
      numsDisplay.text("0");
      _model.numCount = 0;

    },
    //checks if the user typed more numbers then the screen can hold
    checkForError: function() {
      if (_model.numCount > 10 || _model.eqCount > 25) {
        _view.printError();
      }
    },
    //calculates the total equasion entered by the user
    calculateTotal: function(eqArray) {
      let total;
      const window = null,
        self = null,
        document = null,
        $ = null,
        jQuery = null;
      try {
        total = eval(eqArray.join(""));
      } catch (e) {
        console.log("" + e);
      }

      if (total % 1 !== 0) {
        total = total.toFixed(3);
      }
      _view.printNums(total);
      _view.printEq([""]);
      _model.num = "";
    }

  };

  return {
     //checks that first button clicked is a number
    checkNumCount: function(button) {
      console.log(button);
      if (_model.numCount === 0 && !_model.isNumber(button)) {
        numsDisplay.text("0");
        eqDisplay.text("0");
        _model.num = "";
        _model.numCount = 0;
        _model.eqCount = 0;
        _model.nums = [];
        _model.equasion = [];
      } else {
        _controller.findType(button);
      }
    }
  }
})();

$(document).ready(function() {
  $(".button").click(function(e) {
    const target = $(e.target);
    let button = target.data("name");
    Calculator.checkNumCount(button);
  });
});
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console