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.

            
              <!-- HTML & CSS for this project is made by https://codepen.io/merkund/pen/rQjKGj?page=4, all credits due  -->  


<div class="container">
    <p>Too lazy to calculate, eh?*&nbsp;🙈</p>
  <div class="calc">
    <div class="calc-display">0</div>
    <!--Keys-START-->
    <div class="calc-key">
      <button class="key-op" data-action="add">+</button>
      <button class="key-op" data-action="substract">-</button>
      <button class="key-op" data-action="multiply">&times;</button>
      <button class="key-op" data-action="divide">÷</button>
      <button class="numpad">7</button>
      <button class="numpad">8</button>
      <button class="numpad">9</button>
      <button class="numpad">4</button>
      <button class="numpad">5</button>
      <button class="numpad">6</button>
      <button class="numpad">1</button>
      <button class="numpad">2</button>
      <button class="numpad">3</button>
      <button class="numpad">0</button>
      <button class="numpad">.</button>
      <button data-action="clear">AC</button>
      <button class="key-eq" data-action="equate">=</button>
    </div>
    </div>
    <!-- <div class="themer">
    <button class="switch opt1"></button>
      <button class="switch opt2"></button>
      <button class="switch opt3"></button>
    </div>
    <p class="small-text">Try changing colours, too!*</p> -->
    </div>
    </div>
            
          
!
            
              /*  HTML & CSS for this project is made by https://codepen.io/merkund/pen/rQjKGj?page=4, all credits due */

@import url("https://fonts.googleapis.com/css?family=Quicksand:400,500");
html {
  box-sizing: border-box;
}

*,
*::before,
*::after {
  box-sizing: inherit;
}

body {
  align-items: center;
  display: flex;
  height: 100vh;
  justify-content: center;
}

body,
p {
  margin: 0;
}

html {
  display: grid;
  min-height: 100%;
}

body {
  display: grid;
  font-family: "Quicksand", sans-serif;
  background: #8EC5FC linear-gradient(62deg, #8EC5FC 0%, #E0C3FC 100%);
}

.container {
  max-width: 20em;
}

.container > p {
  text-align: center;
}

p {
  color: #333;
  font-size: 2em;
}

.small-text {
  margin-top: 1em;
  font-size: 1.5em;
}

.mini-text {
  font-size: 0.9em;
  margin-top: 1em;
}

.calc {
  background: #ffffff;
  margin: auto;
  position: relative;
  height: 24em;
  width: 20em;
  margin-top: 2em;
  margin-bottom: 2em;
  overflow: hidden;
  border-radius: 20px;
  box-shadow: 0 0 40px 0px rgba(0, 0, 0, 0.3);
}

.calc-display {
  background: #ffffff;
  color: #000;
  font-size: 2.5em;
  padding: 0.5em 0.75em;
  text-align: right;
}

.calc-key {
  display: grid;
  grid-gap: 0px;
  grid-template-columns: repeat(4, 1fr);
}

.calc-key > * {
  padding: 1.25em 1.25em;
  position: relative;
  text-align: center;
}

.calc-key > *:active::before,
.calc-key > .is-depressed::before {
  background: #8EC5FC;
  content: "";
  bottom: 0;
  left: 0;
  right: 0;
  top: 0;
  opacity: 0.3;
  position: absolute;
  z-index: 1;
}

button {
  border: 0;
  border-radius: 0;
  background: transparent;
  font-size: inherit;
  font-family: inherit;
  font-weight: inherit;
  outline: none;
  appearance: none;
  text-align: left;
}

button:hover,
button:active,
button:focus {
  outline: none;
}

.key-op {
  background: #eee;
  font-size: 1.25em;
  padding: 0.8em;
}

.key-eq {
  background: #333;
  color: #ffffff;
  grid-column: -2;
  grid-row: 2/span 4;
  font-size: 1.25em;
}

.themer {
  margin: auto;
  display: flex;
  justify-content: space-between;
  max-width: 10em;
}

.switch {
  padding: 1.1em;
  border-radius: 50%;
}

.opt1 {
  background: #2196f3;
}

.opt2 {
  background: #9c27b0;
}

.opt3 {
  background: #e91e63;
}

            
          
!
            
              // HTML & CSS for this project is made by https://codepen.io/merkund/pen/rQjKGj?page=4, all credits due

// Feture Wishlist
// Display used operator along with number in UI
// -/+ Operator in Calculator


class Calc {
    static reset() {
        Calc.sum = undefined;
        Calc.operatorMemory = undefined;
        Calc.equated = undefined;
    }
    static add(a, b) {
        return a + b;
    }
    static substract(a, b) {
        return a - b;
    }
    static multiply(a, b) {
        return a * b;
    }
    static divide(a, b) {
        return a / b;
    }

    static operate(input, operator) {

        //debugger;

        if (Calc.operatorMemory === undefined) {
            Calc.operatorMemory = operator;
        }

        if (Calc.sum === undefined) {
            Calc.sum = input;

        }

        // Addresses User Continuing Calculation after equating, ie for input 2+5=(7)*2= can return 14.
        else if (Calc.equated && Number.isNaN(input)) {
                Calc.equated = false;
        }
        // Addresses User Switching Operator in Between Calculation, ie for input 2+*5 will return 2*5;
        else if(Number.isNaN(input) && Calc.sum != undefined){
            Calc.operatorMemory = operator;
            return;
        }
        else {

            // Run Calculation
            Calc.sum = Calc[Calc.operatorMemory](Calc.sum, input);

            // Check if Operator is not equate
            if (!(operator === 'equate')) {
                // Updating Operator Memory to Newly Selected Operator for Next Calculation
                Calc.operatorMemory = operator;
                Calc.equated = false;
            } else {
                // Reset Operator Memory 
                Calc.operatorMemory = undefined;
                Calc.equated = true;
            }

        }
        return (Calc.sum);

    }
}
// Declaring Calc Variables
Calc.sum;
Calc.operatorMemory;
Calc.equated;

class Event {
    static captureInput() {
        Event.inputSource.addEventListener('click', (e) => {


            // Check if User Clicked on a Number and then Capture Number as string
            if (e.target.className === 'numpad') {
                
                // Check if Last Input was Equate, If so Reset Calc for Fresh Calculation
                if(Event.inputMemory === 'equate'){
                    Event.reset();
                    Calc.reset();
                    UI.display(0);
                }

                // Adding on to Event.inputString the input values

                // This code is to prevent multiple decimal points in input
                // Check if Input is Decimal
                if (/\./.test(e.target.textContent)) {
                    // Check if Event.input already has a Decimal to Avoid Multiple Decimal Points
                    if (!(/\./.test(Event.inputString))) {
                        // Check if user input is within UI Display Limit
                        if(Event.inputString.length < UI.displayLimit){Event.inputString += e.target.textContent}
                    }
                } else /*If Input is a number, skips the decimal logic*/ {
                    // Check if user input is within UI Display Limit
                    if(Event.inputString.length < UI.displayLimit){Event.inputString += e.target.textContent}
                }

                // Display Input on UI
                UI.display(Event.inputString);

                // Since Clicked Event is Numpad, Sets lnput Memory as Number. See Line 107 and 119 for Clarity
                Event.inputMemory = 'number';

            }
            // Check if User Clicked on an Operator and Run Operation
            else if (e.target.className === 'key-op' || e.target.className === 'key-eq') {

                // To Prevent Same Operator being Clicked Multiple times, see line 119 and 107 for clarity
                if(!(Event.inputMemory === e.target.dataset.action)){
                    Event.selectedOperator = e.target.dataset.action;

                    // Convert Input String to Floating Point Number, Run Calculation & Display in UI 
                    console.log(Event.inputString,Event.selectedOperator);
                    UI.display(Calc.operate(parseFloat(Event.inputString), Event.selectedOperator));
    
                    // Clear Input String
                    Event.inputString = '';
    
                    // Saves Clicked Operator to Memory
                    Event.inputMemory = e.target.dataset.action;
                } 

            }
            // If User Clicked On Clear Reset Calc
            else if (e.target.dataset.action === 'clear') {
                Event.reset();
                Calc.reset();
                UI.display(0);
            }
        });
    }

    static reset(){
        Event.inputString = '';
        Event.selectedOperator = undefined;
        Event.inputMemory = '';
    }

    static init() {
        document.addEventListener("DOMContentLoaded", function () {
            console.log('Dom Loaded');
            Event.captureInput();
        });
    }
}
// Declaring Event Variables
Event.inputSource = document.querySelector('.calc-key');
Event.inputString = '';
Event.selectedOperator;
Event.inputMemory = '';

class UI {
    static display(value) {
        if(typeof(value)==="string"){
            // Limit String to Display Limit of Calculator
            UI.displayBar.textContent = value.substring(0, UI.displayLimit);
        }
        else if(typeof(value)==="number"){
            // Check if Computed Number is within UI display Limit
            if(value < ((Math.pow(10,UI.displayLimit))-1)){
    
                // Compute Trailing Decimals That can be shown without UI Overflow
                UI.displayableDecimalTrail =  (UI.displayLimit) - (value.toString().split(".")[0].length);
                // Limit Trailing Decimal's Digits
                if(value %1 != 0){
                    UI.displayBar.textContent = parseFloat(value.toFixed(UI.displayableDecimalTrail)); // parseFloat used to remove insignificant trailing zeros
                } else {
                    UI.displayBar.textContent = value;
                } 
        
                // If number is more than UI display Limit, Show Error
                } else {
                UI.displayBar.textContent = 'E';
                }
        }
    }
}
// Declaring UI Variables
UI.displayBar = document.querySelector('.calc-display');
UI.displayLimit = 12;
UI.displayableDecimalTrail;



// Listen For Input
Event.init(); 
            
          
!
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