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

              
                <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!--<script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>-->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
    <script>
        let display = document.getElementById("display");
        let trailingResult = 0;
        let operators = ["add","subtract","multiply","divide"];
        let workingOperator = "";
        function updateDisplay(input) {
            let display = document.getElementById("display");

            if(display.innerHTML == 0) {
                if(input ==="decimal"){
                    display.innerHTML = "0."
                } else {
                    display.innerHTML = input;
                }
            } else if(operators.indexOf(input) >= 0){
                if (trailingResult == display.innerHTML){
                    //this is the section thats not working
                    workingOperator = input;
                    console.log("working with a double");
                } else if(workingOperator === "") {
                console.log("working with an operator");
                workingOperator = input;
                trailingResult = display.innerHTML;
                display.innerHTML = 0;
                } else {

                    trailingResult = calculate((trailingResult),(display.innerHTML),workingOperator);;
                    display.innerHTML=trailingResult;
                    display.innerHTML = 0;
                    workingOperator = input;
                }

///         } //// else if(input === "add") {
//                trailingResult = display.innerHTML;
//                display.innerHTML = 0;
//                console.log(trailingResult);
               } else if (input === "equals"){
                //let result = parseFloat(trailingResult) +  parseFloat(display.innerHTML);
                 let result = calculate((trailingResult),(display.innerHTML),workingOperator);
                display.innerHTML = result;
                console.log(result);
                } else if (input === "decimal"){
            //let result = parseFloat(trailingResult) +  parseFloat(display.innerHTML);
//            let result = calculate((trailingResult),(display.innerHTML),workingOperator);
//            display.innerHTML = result;
            console.log("decimal clicked");
            if (display.innerHTML.indexOf(".") === -1){
                display.innerHTML += ".";
            }
        }
            else {
                display.innerHTML += input;
            }
            console.log("display = " +display.innerHTML+"  trailing result ="+trailingResult + "  working eleement = " +workingOperator + "value = " + input);
        }
        function clearDisplay() {
            var display = document.getElementById("display");
            display.innerHTML = 0;
            trailingResult = 0;
            workingOperator = "";
        }

        function calculate (first,second,op) {
            let result;
            first = parseFloat(first);
            second = parseFloat(second);
            switch(op) {
                case "add":
                    result = first + second;
                    return result;
                    break;
                case "subtract":
                    result = first - second;
                    return result;
                    break;
                case "multiply":
                    result = first * second;
                    return result;
                    break;
                case "divide":
                    result = first / second;
                    return result;
                    break;
                default:
                    return result;
                }
        }
    </script>
</head>
<body>
<div class="container">
    3 + 5 * 6 - 2 / 4
<div class="row text-centre">
    <div id="display" class="bg-info text-center">
        0
    </div>
    <button class="btn btn-primary" onclick="clearDisplay()" id="clear">Clear</button>

</div>

<div class="row text-center">
    <div class="col">
        <button class="btn btn-block btn-primary" id="one" onclick="updateDisplay(1)">1</button>
        <button class="btn btn-block btn-primary" id="two" onclick="updateDisplay(2)">2</button>
        <button class="btn btn-primary btn-block" id="three" onclick="updateDisplay(3)">3</button>
    </div>
</div>

<div class="row text-center">
    <div class="col">

        <button class="btn btn-block btn-primary" onclick="updateDisplay(4)" id="four">4</button>
        <button class="btn btn-block btn-primary" onclick="updateDisplay(5)" id="five">5</button>
        <button class="btn btn-block btn-primary" onclick="updateDisplay(6)" id="six">6</button>
    </div>
</div>
<div class="row text-center">
    <div class="col">

        <button class="btn btn-block btn-primary" onclick="updateDisplay(7)" id="seven">7</button>
        <button class="btn btn-block btn-primary" onclick="updateDisplay(8)" id="eight">8</button>
        <button class="btn btn-block btn-primary" onclick="updateDisplay(9)" id="nine">9</button>
    </div>
</div>
<div class="row text-center">
    <div class="col">
        <button class="btn btn-block btn-primary" onclick="updateDisplay('equals')" id="equals">=</button>
        <button class="btn btn-block btn-primary" onclick="updateDisplay(0)" id="zero">0</button>
            <button class="btn btn-block btn-primary" id="decimal" onclick="updateDisplay('decimal')">.</button>
    </div>
</div>
<div class="row text-center">
    <div class="col">
        <button class="btn btn-block btn-primary" onclick="updateDisplay('add')" id="add">+</button>
        <button class="btn btn-block btn-primary" id="subtract" onclick="updateDisplay('subtract')">-</button>
        <button class="btn btn-block btn-primary" id="multiply" onclick="updateDisplay('multiply')">*</button>
        <button class="btn btn-block btn-primary" id="divide" onclick="updateDisplay('divide')">/</button>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>
<script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>
</div>
</body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                
              
            
!
999px

Console