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

              
                <div id="calc_outside">
  <div class="spacer">&nbsp;</div>
  <div id="calc_inside">
    <div class="spacer">&nbsp;</div>
    <div id="calc_display_surface">
      <div id="calc_solar_cell">&nbsp;</div>
      <header><h3>Solar Calculator</h3></header>
      <main id="calc_screen">
        <div id="calc_expression">&nbsp;</div>
        <div id="calc_result">0</div>
      </main>
    </div>
    <menu id="calc_buttons">
      <div id="calc_row_0">
        <button id="calc_b_pi" class="btn">&pi;</button>
        <button id="calc_b_euler" class="btn">E</button>
        <button id="calc_b_delete" class="btn">DEL</button>
        <button id="calc_b_clear" class="btn">CLR</button>
      </div>
      <div id="calc_row_1">
        <button id="calc_b_modulus" class="btn">%</button>
        <button id="calc_b_lparen" class="btn">(</button>
        <button id="calc_b_rparen" class="btn">)</button>
        <button id="calc_b_div" class="btn">/</button>
      </div>
      <div id="calc_row_2">
        <button id="calc_b_7" class="btn">7</button>
        <button id="calc_b_8" class="btn">8</button>
        <button id="calc_b_9" class="btn">9</button>
        <button id="calc_b_mult" class="btn">*</button>
      </div>
      <div id="calc_row_3">
        <button id="calc_b_4" class="btn">4</button>
        <button id="calc_b_5" class="btn">5</button>
        <button id="calc_b_6" class="btn">6</button>
        <button id="calc_b_sub" class="btn">-</button>
      </div>
      <div id="calc_row_4">
        <button id="calc_b_1" class="btn">1</button>
        <button id="calc_b_2" class="btn">2</button>
        <button id="calc_b_3" class="btn">3</button>
        <button id="calc_b_add" class="btn">+</button>
      </div>
      <div id="calc_row_5">
        <button id="calc_b_0" class="btn">0</button>
        <button id="calc_b_decimal" class="btn">.</button>
        <button id="calc_b_equals" class="btn btn_wide">=</button>
      </div>
    </menu>
    <div class="spacer">&nbsp;</div>
  </div>
  <div class="spacer">&nbsp;</div>
</div>

              
            
!

CSS

              
                body {
  background:  linear-gradient(to top, #EEE 20%, #CCC 80%) fixed;
  font-family: 'PT Sans', sans-serif;
}
header {
  margin-bottom: 0.5em;
  color: #CCC;
}


.btn {
  background: #DDD;
  font-size: 110%;
  font-weight: bold;
  padding: 0.5em;
  margin: 0.3em;
  width: 4em;
  border: outset 2px rgba(204,204,204, 0.75);
  border-radius: 0.6em;
  box-shadow: 1px 2px 3px 1px #353535;
}
.btn:active {
  background: #CCC;
  border: inset 2px #CCC;
  box-shadow: inset 1px 2px 3px 1px #353535;
}
.btn:focus {
  outline: none;
  background: #CCC;
}
.btn_wide {
  width: 8.6em;
}
.spacer {
  font-size: 50%;
}


#calc_outside {
  background: #284965;
  text-align: center;
  margin: 4em auto;
  min-width: 400px;
  max-width: 400px;
/*   height: 600px; */
  border-radius: 1.4em;
  box-shadow:
    -1px -2px 3px #252525,
    2px 3px 3px 1px #3A3A3A;
/*   transform: rotate3d(5, 2, -2, 10deg); */
}
#calc_inside {
  background: #385975;
  margin: 0.3em 0.9em 0.4em 1em;
  border-radius: 1.2em;
  box-shadow:
    0px 0px 5px 4px #385975,
    2px 2px 5px 2px #284965;
}
#calc_display_surface {
  background: #182425;
  padding: 0.6em;
  margin: 0.5em 1em;
  border: inset 1px #182425;
  border-radius: 0.3em;
  box-shadow:
    inset 1px 1px 4px #222,
    inset -1px -1px 2px #222;
}
#calc_solar_cell {
  background-color: #26201C;
  background: repeating-linear-gradient(
    to left,
    #111,
    #111 2px,
    #26201C 2px,
    #26201C 29px
  );
  padding: 1em;
  margin: 0.5em 6em 0;
  border-radius: 0.9em 0.88em 0.4em 0.4em / 2em 2em 0.5em 0.5em;
  box-shadow:
    inset 2px 3px 4px #111,
    inset -1px -1px 2px #111;
}
#calc_screen {
  background: #687;
  color: #100A09;
  font-family: 'VT323', monospace;
  text-align: right;
  padding: 1em 1em 0.8em;
  border-radius: 0.3em;
  box-shadow:
    inset 2px 3px 4px #111,
    inset -1px -1px 2px #111;
}
#calc_expression {
  font-size: 175%;
  height: 2.1em;
  padding-bottom: 0.4em;
}
#calc_result {
  font-size: 200%;
  height: 1.1em;
}
#calc_buttons {
  padding: 0.6em;
}



              
            
!

JS

              
                /**
 Core Requirements:
   Can do basic arithmetic
   Has button to clear input field
   Operations can be chained
 Additional Requirements:
   Operations will resolve with operator precedence
   Solar panel
   Buttons:
     0-9 /*-+.=()
     DEL CLEAR
   Errors for invalid inputs:
     Cannot divide by 0
     Need closing paranthesis
     Invalid operator
       sequential operators except for *- /- +- -+
       operators at beginning of expression
 Possible future goals:
   (-) ^ sqrt modulus
   LEFT RIGHT {to modify digits}
   UP DOWN {to access history}
   , [ ] to do arithmetic on a set
   Set decimal places
   Scientific notation
*/


let expression = [' '];

function pushToExpr(str) {
  if (document.getElementById("calc_result").textContent !== '0') {
    if (expression[expression.length - 1] === ')') {
      expression.push('*');
      document.getElementById("calc_expression").textContent += '*';
    }
    expression.push(document.getElementById("calc_result").textContent);
    expression.push(str);
    document.getElementById("calc_expression").textContent += str +
                        document.getElementById("calc_result").textContent;
    document.getElementById("calc_result").textContent = '0';
  } else if (str === '-') {
    expression.push(str);
    document.getElementById("calc_expression").textContent = str;
  } else if (/[ \-\+\/\*\%/(]/.test(expression[expression.length - 1])) {
    if (str === '(' || str === ')' ) {
      expression.push(str);
      document.getElementById("calc_expression").textContent = str;
    }
  } else {
    expression.push(str);
    document.getElementById("calc_expression").textContent = str;
  }
  document.getElementById("calc_expression").innerHTML = expression.join('');
  //console.log(expression);
}

function pushToRes(str) {
  if (/[ \-\+\/\*\%\(\)]/.test(expression[expression.length - 1])) {
    if (document.getElementById("calc_result").textContent === '0') {
      document.getElementById("calc_result").textContent = str;
    } else {
      document.getElementById("calc_result").textContent += str;
    }
  }
}

function evaluateExpression() {
  if (document.getElementById("calc_result").textContent !== '0') {
    expression.push(document.getElementById("calc_result").textContent);
    document.getElementById("calc_expression").innerHTML = expression.join('');
  }
  
  let result = eval(expression.join('')).toString();
  let dec = result.indexOf('.');
  if (dec !== -1) {
    result = result.substr(0, Math.min(result.length - 1, dec + 7));
  }
  
  document.getElementById("calc_result").textContent = result;
  expression = [' '];
}

document.getElementById("calc_b_pi").addEventListener('click', function(el) {
  pushToRes(Math.PI.toString().substr(0,7));
});

document.getElementById("calc_b_euler").addEventListener('click', function(el) {
  pushToRes(Math.E.toString().substr(0,7));
});

document.getElementById("calc_b_delete").addEventListener('click', function(el) {
  if (document.getElementById("calc_result").textContent !== '0') {
    let num = document.getElementById("calc_result").textContent;
    document.getElementById("calc_result").textContent = num.substr(0, num.length-1);
    if (document.getElementById("calc_result").textContent === '')
      document.getElementById("calc_result").textContent = '0';
  } else if (document.getElementById("calc_expression").textContent !== '') {
    // Removes either a single operator or a complete number from the expression
    expression.pop();
    document.getElementById("calc_expression").innerHTML = expression.join('');
  }
});

document.getElementById("calc_b_clear").addEventListener('click', function(el) {
  expression = [' '];
  document.getElementById("calc_expression").textContent = ' ';
  document.getElementById("calc_result").textContent = '0';
});

document.getElementById("calc_b_lparen").addEventListener('click', function(el) {
  // console.log(/[ \-\+\/\*\%]/.test(expression[expression.length - 1]));
  if (document.getElementById("calc_result").textContent !== '0' ||
      /[^\-\+\/\*\%]/.test(expression[expression.length - 1]))
    pushToExpr('*');
  pushToExpr('(');
});

document.getElementById("calc_b_rparen").addEventListener('click', function(el) {
  let expr = document.getElementById("calc_expression").textContent;
  let p = expr.match(/\(/g);
  let q = expr.match(/\)/g);
  // console.log(p, " ", q);
  if (p === null || (q !== null && p.length <= q.length)) {
    let num = document.getElementById("calc_result").textContent;
    document.getElementById("calc_result").textContent = "Error: Unbalanced Parens";
    setTimeout(function() {
      document.getElementById("calc_result").textContent = num;
    }, 500);
  } else if (document.getElementById("calc_result").textContent !== '0' ||
             /[^\-\+\/\*\%]/.test(expression[expression.length - 1])) {
    pushToExpr(')');
  }
});

document.getElementById("calc_b_modulus").addEventListener('click', function(el) {
  pushToExpr('%')
});

document.getElementById("calc_b_div").addEventListener('click', function(el) {
  pushToExpr('/')
});

document.getElementById("calc_b_mult").addEventListener('click', function(el) {
  pushToExpr('*')
});

document.getElementById("calc_b_sub").addEventListener('click', function(el) {
  pushToExpr('-')
});

document.getElementById("calc_b_add").addEventListener('click', function(el) {
  pushToExpr('+')
});

document.getElementById("calc_b_equals").addEventListener('click', function(el) {
  evaluateExpression();
});

document.getElementById("calc_b_decimal").addEventListener('click', function(el) {
  if (document.getElementById("calc_result").textContent === '0')
    pushToRes('0.');
  else if (!/[.]/.test(document.getElementById("calc_result").textContent))
    pushToRes('.');
});

document.getElementById("calc_b_0").addEventListener('click', function(el) {
  if (document.getElementById("calc_result").textContent === '0')
    pushToRes('0.')
  else
    pushToRes('0')
});

document.getElementById("calc_b_1").addEventListener('click', function(el) {
  pushToRes('1')
});

document.getElementById("calc_b_2").addEventListener('click', function(el) {
  pushToRes('2')
});

document.getElementById("calc_b_3").addEventListener('click', function(el) {
  pushToRes('3')
});

document.getElementById("calc_b_4").addEventListener('click', function(el) {
  pushToRes('4')
});

document.getElementById("calc_b_5").addEventListener('click', function(el) {
  pushToRes('5')
});

document.getElementById("calc_b_6").addEventListener('click', function(el) {
  pushToRes('6')
});

document.getElementById("calc_b_7").addEventListener('click', function(el) {
  pushToRes('7')
});

document.getElementById("calc_b_8").addEventListener('click', function(el) {
  pushToRes('8')
});

document.getElementById("calc_b_9").addEventListener('click', function(el) {
  pushToRes('9')
});

              
            
!
999px

Console