Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Calculator</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <div class="calculator">
    <div class="input" id="display">0</div>
    <div class="button">
      <div class="operators">
        <div class="operator" id="add">+</div>
        <div class="operator" id="subtract">-</div>
        <div class="operator" id="multiply">&times;</div>
        <div class="operator" id="divide">&divide;</div>
      </div>
      <div class="leftPanel">
        <div class="numbers">
          <div class="number" id="seven">7</div>
          <div class="number" id="eight">8</div>
          <div class="number" id="nine">9</div>
        </div>
        <div class="numbers">
          <div class="number" id="six">6</div>
          <div class="number" id="five">5</div>
          <div class="number" id="four">4</div>
        </div>
        <div class="numbers">
          <div class="number" id="three">3</div>
          <div class="number" id="two">2</div>
          <div class="number" id="one">1</div>
        </div>
        <div class="numbers">
          <div class="number" id="decimal">.</div>
          <div class="number" id="zero">0</div>
          <div class="clear" id="clear">C</div>
          <div class="backspace" id="backspace">←</div>
        </div>
      </div>
      <div class="equals" id="equals">=</div>
    </div>
  </div>
  <br />
  <p class="text-center">Designed by Linda Aluso</p>
  <script src="script.js"></script>
</body>
 <script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>
     
</html>

 
              
            
!

CSS

              
                body {
    font-family: sans-serif;
    font-size: 2.0em;
    letter-spacing: 5px;
    width: 500px;
    margin: 5% auto;
    -moz-user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
}

.calculator {
    padding: 20px;
    border-radius: 1px;
    box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.2);
}

.input {
    height: 60px;
    border: 1px solid #b9b9b9;
    border-radius: 5px;
    padding-top: 10px;
    padding-right: 15px;
    margin-right: 6px;
    font-size: 15px;
    text-align: right;
    overflow-x: auto;
    transition: all .2s ease-in-out;
}

.input:hover {
    border: 1px solid #bbb;
    box-shadow: inset 0 1px 4px 0 rgba(0, 0, 0, 0.25);
}

.button {
}

.operators {
}

.leftPanel {
    display: inline-block;
}

.operators div, .numbers div {
    display: inline-block;
    border: 1px solid #bbb;
    cursor: pointer;
    width: 100px;
    font-size: 20px;
    padding: 10px;
    margin: 20px 4px 10px 0;
    text-align: center;
}

.operators div {
    background: #ddd;
}

.operators div:hover, .numbers div:hover {
    background: red;
    border-color: #fff;
    box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.2);
}

.operators div:active, .numbers div:active {
    font-weight: bold;
}

.equals {
    display: inline-block;
    vertical-align: top;
    width: 20%;
    background: #4d90fe;
    color: #ddd;
    padding: 20px;
    margin: 10px 6px 10px 0;
    border: 1px solid #c9c9c9;
    text-align: center;
    cursor: pointer;
}

.equals:hover {
    border: 1px solid #d6d6d6;
    box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.2);
}

.equals:active {
    font-weight: bold;
}

  
              
            
!

JS

              
                $(document).ready(function() {
  var currentInput = "";  // Store the current input string
  var displayValue = "0";  // Store the value to be displayed

  // Valid operators (normal and decimal)
  var validOperators = {
    normal: ["+", "-", "*", "/"],
    decimal: ["."]
  };

  // Handle input button clicks
  $(".number, .operator").on("click", function() {
    var value = $(this).text();
    
    // Map the symbols to correct operators for calculation
    if (value === "×") {
      value = "*";
    } else if (value === "÷") {
      value = "/";
    }
    
    handleInput(value);
  });

  // Clear the input
  $("#clear").on("click", function() {
    currentInput = "";
    updateDisplay();
  });

  // Remove the last character
  $("#backspace").on("click", function() {
    currentInput = currentInput.slice(0, -1);  // Remove the last character
    updateDisplay();
  });

  // Calculate the total
  $("#equals").on("click", function() {
    calculateTotal();
  });

  // Handles input and updates the display
  function handleInput(value) {
    var lastChar = currentInput[currentInput.length - 1];

    // Prevent multiple decimals
    if (validOperators.decimal.includes(value) && lastChar === ".") {
      return;  // Halt if there's already a decimal point
    }

    // Prevent starting with an operator
    if (currentInput === "" && validOperators.normal.includes(value)) {
      return;
    }

    // Append value to the current input
    currentInput += value;
    updateDisplay();
  }

  // Update the display
  function updateDisplay() {
    $("#display").text(currentInput || "0");
  }

  // Calculate and display the total
  function calculateTotal() {
    try {
      var result = eval(currentInput);  // Use eval to calculate the expression
      if (result !== undefined) {
        currentInput = result.toString();  // Store result for further operations
        updateDisplay();
      }
    } catch (error) {
      currentInput = "Error";
      updateDisplay();
    }
  }
});

              
            
!
999px

Console