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

              
                <input type="button" value="=" id="equals" class="noDisplay" onclick="equals()">
<!-- Additional Hidden Equals Button for Test Pass (non-functional) -->

<h1 id="heading">Javascript Calculator</h1>
 
<div class="calculator">
 
<input type="text" id="display" class="calculator-screen" value="0">

<div class="calculator-keys">
 
<input type="button" class="operator" value="+" id="add" onclick="pushToScreen('+')">
<!-- Add Button -->

<input type="button" value="-" id="subtract" class="operator" onclick="pushToScreen('-')">
<!-- Subtract Button -->

<input type="button" value="&times;" id="multiply" class="operator" onclick="pushToScreen('*')">
<!-- Multiply Button -->
 
<input type="button" value="&divide" id="divide" class="operator" onclick="pushToScreen('/')">
<!-- Divide Button -->

 <input type="button" value="7" id="seven" onclick="pushToScreen('7')">
<input type="button" value="8" id="eight" onclick="pushToScreen('8')">
<input type="button" value="9" id="nine" onclick="pushToScreen('9')">
      
<input type="button" value="4" id="four" onclick="pushToScreen('4')">
<input type="button" value="5" id="five" onclick="pushToScreen('5')">
<input type="button" value="6" id="six" onclick="pushToScreen('6')">
      
     
<input type="button" value="1" id="one" onclick="pushToScreen('1')">
<input type="button" value="2" id="two" onclick="pushToScreen('2')">
<input type="button" value="3" id="three" onclick="pushToScreen('3')">
      
<input type="button" value="0" id="zero" onclick="pushToScreen('0')">
     
<input type="button" value="." class="decimal" id="decimal" onclick="pushToScreen('.')">
<!-- Decimal Button -->

<input type="button" value="Clear" class="all-clear" id="clear"onclick="pushToScreen('c')">
<!-- Clear Button -->
 
<input type="button" value="=" id="equalsStyle" class="equal-sign" onclick="equals()">
<!-- Equals Button -->

    </div>
  </input>

<h2> here is my calculator!!!</h2>
              
            
!

CSS

              
                html {
  font-size: 62.5%;
  box-sizing: border-box;
}

h1 {
  font-size: 40px;
  text-align:center;
}

h2 {position:absolute;
font-size:20px;
top:580px;
  left:400px;
}

.noDisplay {display: none !important;}

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

.calculator {
  border: 1px solid #ccc;
  border-radius: 5px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 400px;
}

.calculator-screen {
  width: 100%;
  font-size: 5rem;
  height: 80px;
  border: none;
  background-color: #eaeaea !important;
  color: #fff;
  text-align: right;
  padding-right: 20px;
  padding-left: 10px;
}

.calculator-screen:hover{cursor:default;}

input {
  height: 60px;
  background-color: #fff;
  border-radius: 3px;
  border: 1px solid #c4c4c4;
  background-color: transparent;
  font-size: 2rem;
  color: #333;
  background-image: linear-gradient(to bottom,transparent,transparent 50%,rgba(0,0,0,.04));
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.05), inset 0 1px 0 0 rgba(255,255,255,.45), inset 0 -1px 0 0 rgba(255,255,255,.15), 0 1px 0 0 rgba(255,255,255,.15);
  text-shadow: 0 1px rgba(255,255,255,.4);
}

input:hover {
  background-color: #eaeaea;
 cursor: pointer;
}

.operator {
  color: #337cac;
 
}

.all-clear {
  background-color: #f0595f;
  border-color: #b0353a;
  color: #fff;
}

.all-clear:hover {
  background-color: #f17377;
}

.equal-sign {
  background-color: #2e86c0;
  border-color: #337cac;
  color: #fff;
  height: 100%;
  grid-area: 2 / 4 / 6 / 5;
}

.equal-sign:hover {
  background-color: #4e9ed4;
}

.calculator-keys {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-gap: 20px;
  padding: 20px;
}
              
            
!

JS

              
                // I really like you calculator styling, nice work!! And keep going, so close and good luck!

var answer = document.getElementById("display");

var prevKeys = [];// track each key pressed. It's one button event behind what's displayed in the calculator. Do a console.log(prevKeys) to see.

var numberHistory = []; //used to decide if a complete number was created, if this array length is greater than 0, we can add another decimal to the formula. the array is cleared after a decimal is added.

function pushToScreen(x) { 
  var currentKey = x; //new variable named for readability
  var isOperator = /[+\*-/]/g; //regex that's suppose to test for operator keys, but decimal seems to pass as well.
  
  var prevKeysStr = prevKeys.join('').toString(); //the values in the input are ahead by one event, so we keep an array of keys press and convert to a string so we can use substring() or includes()

  var lastKeyIsOperator = isOperator.test(currentKey); // used to test the last key pressed and see if it's an operator. Decimals still pass for some reason, not sure why.
  
  var formulaHasOperatorBeforeEnd = isOperator.test(prevKeysStr.substring(prevKeysStr.length -1, prevKeysStr.length)[0]); // test the last key value in our history (not including current key pressed) is an operator, **reminder** decimals seem to pass
  
  if (isOperator.test(x) && (x !== ".")) { //handle operators here
    numberHistory.push(prevKeysStr); //if an operator was pressed, we add the numbers before it to our array. allowing us to add a decimal again
    
    //needs more work here to finish up all tests, so close!
    // ultimately you want to allow a negative operator after any one other operator, but replace all operators with the newest operator pressed without allowing duplicate operators back-to-back. One way could be to look at prevKey history and use subtring to remove the last one or two operators and combine with the newest operator/currentKey
    if (formulaHasOperatorBeforeEnd && lastKeyIsOperator && currentKey !== "-") {
      answer.value = prevKeysStr.substring(0, prevKeysStr.length -2) + currentKey;
      prevKeys.push(currentKey);
    } else {
      answer.value = answer.value + currentKey;
      prevKeys.push(currentKey);
    }
    
  } else if (x === "c") { //handle clearing button here
    answer.value = "0";
    prevKeys = [];
    
  } else if ((x !== "c") && (x !== ".") && (answer.value === "0")) { //handle first number key pressed after clear
    answer.value = x;
    prevKeys = [];
    prevKeys.push(x);
    
  } else if (x === ".") { // handle decimal behaviors here
    if (!prevKeysStr.includes(".")) { //if no decimals exist in previous button presses
      prevKeys.push(x);
      answer.value += x;
    } else if ( //if the last item in previous keys is one of the operators and not a decimal
      prevKeysStr.substring(prevKeysStr.length -1, prevKeysStr.length) !== '.' &&
        ( prevKeysStr.substring(prevKeysStr.length -1, prevKeysStr.length) == '+' ||
        prevKeysStr.substring(prevKeysStr.length -1, prevKeysStr.length) == '/' ||
        prevKeysStr.substring(prevKeysStr.length -1, prevKeysStr.length) == '-' ||
        prevKeysStr.substring(prevKeysStr.length -1, prevKeysStr.length) == '*')
    ) {
      prevKeys.push(x);
      answer.value += x;
      
    } else if ( // if there's a number in our history, added by pressing an operator, and we didn't have a decimal at the end of all previous keys pressed, we can add a decimal again. then we clear the last number from our history
      numberHistory.length > 0 &&
      prevKeysStr.substring(prevKeysStr.length -1, prevKeysStr.length) !== '.'
    ) {
      prevKeys.push(x);
      answer.value += x;
      numberHistory = []; //clear number history
    }
    
  } else { //normal display updating here
    answer.value += x;
    prevKeys.push(x);
  }
  
  //updates display input in HTML (not necessary/important)
  answer.setAttribute('value', answer.value);
}

function equals() {
  numberHistory = []; //clear number history
  x = answer.value;
  x = eval(x);
  answer.value = x;
}

              
            
!
999px

Console