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

              
                <!--Javascript Calculator-->
<div id="calculator" class="well calculator-body container">
    <h4 style="color: white; padding-top: 20px; padding-bottom: 10px; font-size: 1.5em;">Let Me Solve Your Problems</h4>
    <form>
        <input type="text" id="output" value="༼ つ ◕_◕ ༽つ ༼ つ ◕_◕ ༽つ" disabled>
        <!--Row 1 (Top)-->
        <div class="row center">
            <button type="button" class="btn btn-extra-lg clear" value="CE" id="CE"><span style="margin-left: -6px;">CE</span></button>
            <button style="width: 47%;" type="button" class="btn btn-extra-lg clear" value="AC" id="AC">AC</button>
            <button type="button" class="btn btn-extra-lg operator" value="/" id="divide">÷</button>
        </div>
        <!--Row 2-->
        <div class="row center">
            <button type="button" class="btn btn-extra-lg number" value="7" id="seven">7</button>
            <button type="button" class="btn btn-extra-lg number" value="8" id="eight">8</button>
            <button type="button" class="btn btn-extra-lg number" value="9" id="nine">9</button>
            <button type="button" class="btn btn-extra-lg operator" value="*" id="multiply">X</button>
        </div>
        <!--Row 3-->
        <div class="row center">
            <button type="button" class="btn btn-extra-lg number" value="4" id="four">4</button>
            <button type="button" class="btn btn-extra-lg number" value="5" id="five">5</button>
            <button type="button" class="btn btn-extra-lg number" value="6" id="six">6</button>
            <button type="button" class="btn btn-extra-lg operator" value="-" id="subtract">-</button>
        </div>
        <!--Row 4-->
        <div class="row center">
            <button type="button" class="btn btn-extra-lg number" value="1" id="one">1</button>
            <button type="button" class="btn btn-extra-lg number" value="2" id="two">2</button>
            <button type="button" class="btn btn-extra-lg number" value="3" id="three">3</button>
            <button type="button" class="btn btn-extra-lg operator" value="+" id="add">+</button>
        </div>
        <!--Row 5-->
        <div class="row center">
            <button type="button" class="btn btn-extra-lg number" value="0" id="zero" style="width: 22%;">0</button>
            <button type="button" class="btn btn-extra-lg number" value="." id="decimal" style="width: 22%;">.</button>
            <button style="width: 47%;" type="button" class="btn btn-extra-lg operator" value="=" id="equals">=</button>
        </div>
        <div class="row center">
            <button style="width: 80%; margin-bottom: 20px;" type="button" class="btn btn-extra-lg" id="emotional-support"><span style="margin-left: -8px;">Emotional Support</span></button>
        </div>
    </form>
</div>
              
            
!

CSS

              
                body {
    display: block;
    text-align: center;
    margin: 0 auto;
    overflow: hidden;
    font-family: 'Syncopate', sans-serif;
    background-image: url("https://dl.dropboxusercontent.com/s/xg6mqa90o1hqk4l/numbers-background-footage-008985563_prevstill.jpeg?dl=0");
}

h1,
h2,
h3,
h4,
h5,
h6 {
    font-size: 2.5em;
    padding-bottom: 20px;
    color: white;
}

.btn:focus {
    box-shadow: none;
}

.btn-extra-lg:active {
    border: none;
    outline: none;
}

.center {
    text-align: center;
    margin: auto;
}

.well {
    background-color: rgba(0, 0, 0, 1);
}

.calculator-body {
    width: 350px;
    margin: 50px auto;
    text-align: center;
    border-radius: 40px;
}

.btn:hover {
    background-color: black;
    color: white;
}

.btn-extra-lg {
    padding: 20px 30px;
    margin: 8px auto;
    font-size: 1em;
    line-height: normal;
    border-radius: 8px;
    height: 100%;
    width: 23%;
    text-align: center;
}

.cur-oper {
    background-color: black;
    color: white;
}

#output {
    width: 80%;
    text-align: center;
    font-size: auto;
    margin-bottom: 10px;
}

@media (min-width: 450px) {
    .calculator-body {
        margin-top: 100px;
    }
}
              
            
!

JS

              
                // Global Variables
var calcArr = [];
var calcHistoryArr = [];
var operatorsArr = [];
var calcDisplay = document.getElementById("output");
var randSupNumArr = [];
var calcNumbers = document.getElementsByClassName("number");
var operators = document.getElementsByClassName("operator");
var clearOp = document.getElementsByClassName("clear");
var lastTotVal = [];
var lastEquals = "notPressed";
var partialClear = 0;

// Values from calculator made clickable and able to be passed into calculator function
startUp();

function startUp() {
    for (var i = 0; i < calcNumbers.length; i++) {
        calcNumbers[i].addEventListener("click", numberCombine, false);
    }
    for (var j = 0; j < operators.length; j++) {
        operators[j].addEventListener("click", operatorsCombine, false);
        operatorsArr.push(operators[j].value);
    }
    for (var x = 0; x < clearOp.length; x++) {
        clearOp[x].addEventListener("click", clearCalc, false);
    }
}

// Numbers
function numberCombine(numVal) {
    var curNum = numVal.target.value;
    // If equals sign was pressed and THEN a number was pressed clear the calc and then enter number
    if (lastEquals === "pressed") {
        clearCalc({ target: { value: "AC" } });
    }
    // Make sure 0 can't be entered first or after negative sign  
    if (curNum === "0") {
        if (calcArr.length === 0) {
            return;
        } else if (calcArr.length === 1 && calcArr[0] === "-") {
            return;
        }
    }
    // Check for multiple decimals
    if (curNum === "." && decimalDupFilter(curNum)) {
        return;
    }
    // Push number to calculator total array
    lastEquals = "notPressed";
    calcHistory(curNum);
}

// Operators
function operatorsCombine(opVal) {
    var curOperator = opVal.target.value;
    $(".operator").removeClass("cur-oper");
    // If there is nothing pressed don't let any operators except minus sign display
    if (calcArr.length === 0 && curOperator !== "-") {
        return;
    }
    // If last button pressed was an operator change operator being pressed
    if (checkLastOper()) {
        // If last button operator and current button pressed is equals exit function
        if (curOperator === "=") {
            return;
        }
        if (calcArr.length === 1) {
            $("#subtract").addClass("cur-oper");
            return;
        }
        calcArr.pop();
        calcHistoryArr.pop();
        // If lastTotVal.length is 1 or less then make it an empty array (prevents "cannot read prop of undefined" error)
        lastTotVal.length <= 1 ? lastTotVal = [] : lastTotVal = calcArr[calcArr.length - 1];
        opVal.target.classList.add("cur-oper");
        calcHistory(curOperator);
        return;
    }
    // If equals is pressed call function to evaluate
    if (curOperator === "=") {
        calcHistoryArr.push(curOperator);
        if (multipleOperators()) {
            equalsFunc();
            return;
        } else {
            calcHistoryArr.pop(curOperator);
            return;
        }
    }
    opVal.target.classList.add("cur-oper");
    calcHistory(curOperator);
}

// Clear Buttons
function clearCalc(clearVal) {
    var curClearBtn = clearVal.target.value;
    // CE is pressed    
    if (curClearBtn === "CE") {
        // If there are no numbers left to take away when user presses "CE" set calcArr, calcHistoryArr, and lastTotVal to 0 
        if (calcArr.length <= 1) {
            partialClear = 1;
            clearCalc({ target: { value: "AC" } });
            return;
        }
        calcHistoryArr.pop();
        calcArr.pop();
        lastTotVal = calcArr[calcArr.length - 1];
        calcDisplayFunc(calcArr[calcArr.length - 1]);
        return;
        // Equals is pressed         
    } else if (curClearBtn === "AC") {
        calcArr = [];
        lastTotVal = [];
        calcHistoryArr = [];
        (Boolean(partialClear) ? calcDisplayFunc(0) : calcDisplayFunc('༼ つ ◕_◕ ༽つ ༼ つ ◕_◕ ༽つ ༼ つ ◕_◕ ༽つ'));
        partialClear = 0;
        return;
    }
}

// All Clicked Numbers Array
function calcHistory(lastInput) {
    lastEquals = "notPressed";
    calcHistoryArr.push(lastInput);

    // Add all numbers together if more than 2 operators are present
    if (multipleOperators()) {
        calcDisplayFunc(eval(calcArr[calcArr.length - 1]) + lastInput);

        calcArr.push((eval(calcArr[calcArr.length - 1])).toString(), (eval(calcArr[calcArr.length - 1]) + lastInput));
        lastTotVal = calcArr[calcArr.length - 1];
        calcHistoryArr.splice(-1, 1, "STOP", lastInput);
        return;
    }

    // Adds new number to calcArr
    if (lastTotVal.length !== 0) {
        lastTotVal = lastTotVal.split('');
    }
    lastTotVal.push(lastInput);
    lastTotVal = lastTotVal.join('');
    // Calculator History
    calcArr.push(lastTotVal);
    calcDisplayFunc(calcArr[calcArr.length - 1]);
}

// Displays data to calc display
function calcDisplayFunc(display) {
    $('#output').css('text-align', 'right');
    calcDisplay.value = display;
}

// Evaluates calculator math
function equalsFunc() {
    calcDisplayFunc(eval(lastTotVal));
    calcArr.push(eval(lastTotVal).toString());
    lastTotVal = eval(lastTotVal).toString();
    lastEquals = "pressed";
}

// Check for multiple operators
function multipleOperators() {
    // Copy to compensate for splice
    var calcHistoryArrCopy = calcHistoryArr.slice();
    var result;
    // Search calcHistory for "STOP" markers
    for (var i = calcHistoryArr.length - 1; i >= 0; i--) {
        if (calcHistoryArr[i].match(/\b(STOP)\b/)) {
            // "STOP" will be included in calcHistorySpl array. To exclude it change "calcHistoryArr.splice(i)" to "calcHistoryArr.splice(i + 1)"
            calcHistoryArr = calcHistoryArr.splice(i);
            break;
        }
    }
    // Filter for multiple operators
    result = calcHistoryArr.filter(function(elem) {
        return operatorsArr.indexOf(elem) !== -1;
    }).length > 1;
    // Restores calcHistoryArr after splice  
    calcHistoryArr = calcHistoryArrCopy.slice();
    return result;
}

// Detect duplicates in current total; returns true if duplicate is found
function decimalDupFilter(val) {
    var calcHistStr = calcHistoryArr.join('');
    var operCount = 0;

    for (var i = 0; i < operatorsArr.length; i++) {
        if (calcHistStr.lastIndexOf(val) > calcHistStr.lastIndexOf(operatorsArr[i])) {
            operCount++;
        }
    }
    if (operCount === operatorsArr.length) {
        return true;
    } else {
        return false;
    }
}

// Make sure no operators can be added consecutively
function checkLastOper() {
    if (calcArr.length > 0) {
        return operatorsArr.filter(elem => calcArr[calcArr.length - 1].charAt(calcArr[calcArr.length - 1].length - 1) === elem).length !== 0;
    } else return false;
}

//Emotional Support Button Function
document.getElementById("emotional-support").addEventListener("click", function() {
    var prevNum;
    var emoSupArr = ["You're doing great!!", "Math is hard.", "The sky's the limit!", "Numbers are friends.", "I love you.", "༼ つ ◕_◕ ༽つ ༼ つ ◕_◕ ༽つ ༼ つ ◕_◕ ༽つ"];
    var randEmoSupNum = Math.floor(Math.random() * emoSupArr.length);
    clearCalc({ target: { value: "AC" } });

    while (randEmoSupNum === randSupNumArr[randSupNumArr.length - 1]) {
        randEmoSupNum = Math.floor(Math.random() * emoSupArr.length);
    }
    randSupNumArr.push(randEmoSupNum);
    $('#output').css('text-align', 'center');
    $('#output').val(emoSupArr[randEmoSupNum]);
    prevNum = randEmoSupNum;
});
              
            
!
999px

Console