Pen Settings

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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div id='calculator'>
<!--   <div id='mode'>DEG</div> -->
  <div class='output' id='prev_entry'>
  </div>
  <div class='output' id='prev_eval'>
  </div>
  <div class='output' id='curr_entry'>
  </div>
  <div class='output' id='curr_eval'>
  </div>
<!--   <div class='output' id='entry'><input size='35'></input>
  </div> -->
  <div class='output' id='entry'>
  </div>
  <div class='row'>
    <button class='mode' id='deg'>DEG</button>
    <button id='rad'>RAD</button>  
    <button class='func trig' id='asin'>asin</button>
    <button class='func trig' id='acos'>acos</button>
    <button class='func trig' id='atan'>atan</button>
    <button class='func' id='fact'>!</button>
    <button class='func' id='ans'>Ans</button>
  </div>
  <div class='row'>
    <button class='lside func' id='pi'>&pi;</button>
    <button class='lside func' id='recip'>1/x</button>
    <button class='func trig' id='sin'>sin</button>    
    <button class='func trig' id='cos'>cos</button>
    <button class='func trig' id='tan'>tan</button>
    <button class='other' id='lpar'>(</button>
    <button class='other' id='rpar'>)</button>
  </div>
  <div class='row'>
    <button class='lside func' id='x_to_y'>x<sup>y</sup></button>
    <button class='lside func' id='nth_root'><sup>y</sup>&radic;<span style="text-decoration:overline">x</span>
</button>
</button>
    <button class='nums' id='7'>7</button>
    <button class='nums' id='8'>8</button>
    <button class='nums' id='9'>9</button>
    <button class='ops' id='divide'>&divide</button>
    <button class='other' id='neg'>(-)</button>
  </div>
  <div class='row'>
    <button class='lside func' id='xsq'>x<sup>2</sup></button>
    <button class='lside func' id='sqrt'>&radic;<span style="text-decoration:overline">x </span>
</button>
    <button class='nums' id='4'>4</button>
    <button class='nums' id='5'>5</button>
    <button class='nums' id='6'>6</button>
    <button class='ops' id='mult'>&times;</button>
    <button class='undo' id='clearall'>AC</button>
  </div>
  <div class='row'>
    <button class='lside func' id='e_to_x'>e<sup>x</sup></button>
    <button class='lside func' id='ln'>ln</button>
    <button class='nums' id='1'>1</button>
    <button class='nums' id='2'>2</button>
    <button class='nums' id='3'>3</button>
    <button class='ops' id='sub'>-</button>
    <button class='undo' id='clear'>C</button>
  </div>
  <div class='row'>
    <button class='lside func' id='ten_to_x'>10<sup>x</sup></button>
    <button class='lside func' id='log'>log</button>
    <button class='nums' id='0'>0</button>
    <button class='other' id='dec'>.</button>
    <button class='other' id='EE'>EE</button>
    <button class='ops' id='plus'>+</button>
    <button class='undo' id='back'>&larr;</button>
  </div>
  <div class='row'>
    <button id='equals'>=</button>
  </div>
</div>

<ul id='upcoming'><strong>Upcoming Features:</strong>
  <li>Get the nth root button working</li>
  <li>Allow keyboard entry</li>
  <ul><li>Move cursor to allow inline editing of input</li></ul>
  <li>Add "entry" button to display the previous entry and allow editing (maybe)</li>
  <li>Store more entries in history (maybe)</li>
  <li>Clean up the javascript code</li>
</ul>
            
          
!
            
              body {
  margin-top: 20px;
  background: url('http://parade.com/wp-content/uploads/2013/05/maths-ftr.jpg') fixed;
  background-size: cover;
}

#calculator {
  background-color: #D3D3D3;
  width: 360px;
  height: 530px;
/*   width: 490px; */
/*   height: 730px; */
  border: 1px solid black;
  margin: auto;
  border-radius: 5px;
  padding-top: 20px;
}

.mode {
  background-color: green;
  color: white;
}

#prev_entry, #entry {
  background-color: white;
  width: 95%;
  height: 30px;
  text-align: left;
  margin: auto;
  font-size: 20px;
}

#entry {
  margin-bottom: 10px;
}

input {
  width: 100%;
}

#prev_eval {
  background-color: white;
  width: 95%;
  height: 30px;
  text-align: right;
  margin: auto;
  font-size: 20px;
  border-bottom: 0.5px black solid;  
}

#curr_entry {
  background-color: #F0F0F0;
  width: 95%;
  height: 30px;
  text-align: left;
  margin: auto; 
  font-size: 20px;
}

#curr_eval {
  background-color: #F0F0F0;
  width: 95%;
  height: 30px;
  text-align: right;
  margin: auto;
  font-size: 20px;
  border-bottom: 1px black solid;
}

.row {
  margin-left: 10px;
}

.nums {
  background-color: #5E5E5E;
  color: white;
  font-size: 24px;
}

.ops {
  background-color: #A3A3A3;
  font-size: 24px;
}

.undo {
  background-color: #ff6666;
  font-size: 24px;  
}

.lside {
  background-color: #c4c4ff;
  font-size: 18px; 
  height: 45px;
}

.func {
  background-color: #c4c4ff;  
}

#equals {
  background-color: #c4c4ff;
  line-height: 50px;
  width: 93%;
  height: 70px;
  font-size: 50px;
  margin: auto;
  margin-top: 5px;  
  padding-top: 0px;
}

.other {
  background-color: white;
}

#neg, #dec, #EE {
  height: 45px;
  font-size: 24px;
}

button {
  border-radius: 8px;
  width: 45px;
  text-align: center;
}

#deg, #rad {
  height: 23px;
  padding-top: 0;
}

#upcoming {
  background-color: white;
  width: 70%;
  font-size: 20px;
  margin: auto;
  margin-top: 20px;
}
            
          
!
            
              $(document).ready(function() {
 
  var exp = false;
  var nthRoot = false;
  var convert = 180/Math.PI;
  var angConvert = Math.PI/180;
  var lastClicked = '';
  //show the clicked button in entry 
  $('.func, .other, .nums, .ops, #EE').not('#log, #ln, #sqrt, #nth_root, #ten_to_x').click(function() {

    if ($(this).is('.nums')) {
      lastClicked = $(this).html();
    }
    
    //if operation hit 1st, add Ans to display
    if ($('#entry').is(':empty') && ($(this).is('.ops, #EE, #fact')) && !$('#curr_eval').is(':empty')) {
      $('#entry').html('Ans');
    }
    
    if ($(this).is('#xsq, #recip, #x_to_y')) {
      return;
    }
    
    //if e^x
    if ($(this).is('#e_to_x')) {
      exp = true;
    }
    
    //if not exponent
    if (!exp) {
      $('#entry').append($(this).html().replace('(-)','-').replace('EE', 'E'));
      // $('#entry').val($('#entry').val() + $(this).html())
        //if trig function
      //need to do calculation, adding * if next to number, like I did for pi
      if ($(this).hasClass('trig')) {
        $('#entry').append('(');
      }
    }
    //if yes exponent
    else if (exp) {
      if ($(this).is('#e_to_x')) {
      $('#entry').append($(this).html().replace('<sup>x</sup>',''))
        return;
    }
      
      
      //if x^y hit first, add Ans if needed, then superscript
      if ($('#entry').is(':empty')) {
        $('#entry').html('Ans');
      } 
      //start the superscript
      if (nthRoot) {
        var superRoot = '<sup>' + $(this).html() + '</sup>&radic;(';
        $('#entry').html($('#entry').html().replace('√(',superRoot));
      
      }
      
      
      if ($(this).is('#neg, #dec, .nums') ) {
        $('#entry').append('<sup>' + $(this).html().replace('(-)','-').replace('EE','E'));
      }
      //end the supercript
      if (!$(this).is('#neg, #dec, .nums'))  {
        $('#entry').append('</sup>'+$(this).html());
        exp = false;
      }
    }
    
    
  }) //end of show the clicked button in entry
  
  //change between deg and rad
  $('#deg,#rad').click(function() {
    $('#deg,#rad').removeClass('mode');
    $(this).addClass('mode');
    convert = ($('#deg').hasClass('mode')) ? 180/Math.PI : 1;
    angConvert = ($('#deg').hasClass('mode')) ? Math.PI/180 : 1;
    console.log('convert = ' + convert);
    console.log('angConvert = ' + angConvert)
  })
  
  // var x = $('#rad').is('.mode') ? 2 : 0;
  // console.log(x)
  
  
  
  
  // 1/x function
  $('#recip').click(function() {
    if ($('#entry').is(':empty')) {
        $('#entry').html('Ans');
      }  
    $('#entry').append('<sup>-1</sup>')
  })
  
  
  // xsq function
  $('#xsq').click(function() {
    if ($('#entry').is(':empty')) {
        $('#entry').html('Ans');
      }  
    $('#entry').append('<sup>2</sup>')
  })
  
  
  // exponent function
  $('#x_to_y').click(function() {
    exp = true;
  })
  
  
  //log and ln function
  $('#log, #ln').click(function() {
    $('#entry').append($(this).html() + '(');
  })
  
  
  //10^x
  $('#ten_to_x').click(function() {
    $('#entry').append('10');
    exp = true;
  })
  
  
  //sqrt
  $('#sqrt').click(function() {
    $('#entry').append('&radic;(');
  })
  
  
  //nth root
  // $('#nth_root').click(function() {
  //  var begin = lastClicked ? ' <sup>'+lastClicked+'</sup>': '';
  //  $('#entry').append(begin+'&radic;(');
  // })
  
  $('#nth_root').click(function() {
    $('#entry').append(' &radic;(');
    nthRoot = true;
    exp = true;
    
   
  })
  
  
  //if trig function
    // $('.trig').click(function() {
    //   $('#entry').append($(this).html()+'(');
    // })
  
  
  //when equals is pushed
  $('#equals').click(function() {
    //bump entry up to curr and curr to prev
    $('#prev_entry').html($('#curr_entry').html());
    $('#prev_eval').html($('#curr_eval').html());
    $('#curr_entry').html($('#entry').html());
    
    //do calculation & put in curr_eval
    var ans = $('#curr_eval').html();
    var val = $('#entry').html().replace(/×/g,'*').replace(/÷/g,'/').replace(/(\d|\!)\(/g,'$1*(').replace('Ans',ans).replace(/([\d]+)π/g,"$1*Math.PI").replace(/π/g,Math.PI).replace(/<\/sup><sup>/g,'').replace(/<sup>/g,'^').replace(/<\/sup>/g,'').replace(/(\(?\d*\.*\d*E?-?[\d]+\)?)\^(\-*[.\d]+)/g, "Math.pow($1, $2)").replace(/(\d+|\(.+\)+)\!/g,'fact($1)').replace(/([\d]+)e/g,'$1*e').replace(/e\^(\-*[.\d]+)/g, "Math.pow(Math.E, $1)").replace(/([\d]+)(acos|asin|atan)/g,'$1*$2').replace(/\b(a[a-z]+)/g,"convert*Math.$1").replace(/([\d]+)(cos|sin|tan)/g,'$1*$2').replace(/\b(cos|sin|tan)\(([\d]+)/g,'Math.$1(($2*angConvert)').replace(/\)(convert|Math)/g,')*$1').replace(/([\d]+)log/g,"$1*log").replace(/log/g,'Math.log10').replace(/([\d]+)ln/g,"$1*Math.log").replace(/ln/g,'Math.log').replace(/(\d+|\))√/g,'$1*√').replace(/√/g,'Math.sqrt');
    // .replace(/^\.E/,'e')
    console.log($('#entry').html())
    console.log(val)
    // console.log(convert)
    // console.log(angConvert)
    try {
      eval(val);
      
      var calculation = Number.isInteger(eval(val)) ? (eval(val).toString().indexOf('e') > 0 ? parseFloat(eval(val)) : parseInt(eval(val))) : parseFloat(eval(val).toPrecision(15));
      //$('#curr_eval').html(calculation.toString().replace('e','E'));
    } 
    catch (e) {
      if (e instanceof SyntaxError) {
        alert(e.message); 
        }
    } //end of try stuff
    
    calculation ? $('#curr_eval').html(calculation.toString().replace('e','E')) : $('#curr_eval').empty();

    //clear entry
    $('#entry').empty();
    val = '';
    
    exp = false;
  })//end of equal functionality
  
  
  //clearall function
  $('#clearall').click(function() {
    $('.output').empty();
  })
  
  
  //clear function
  $('#clear').click(function() {
    var remove = ($('#entry').is(':empty')) ? $('#curr_entry,#curr_eval') : $('#entry') ;
    remove.empty();
    //move prev_entry down to curr_entry
    if ($('#curr_entry').is(':empty')) {
      $('#curr_entry').html($('#prev_entry').html());
      $('#curr_eval').html($('#prev_eval').html());
      $('#prev_entry').empty();
      $('#prev_eval').empty();
    }  
  }) //end of clear function
  
  
  //backspace function
  $('#back').click(function() {
    var str = $('#entry').html();
    if (str.indexOf('</sup>') > -1) {
      str = str.substring(0, str.length - 12);
    }
    else {
      str = str.substring(0, str.length - 1);
    }
    $('#entry').html(str);
  }) //end of backspace function
  
}) //end of doc ready

function fact(n) { 
  return n == 0 ? 1 :  n*fact(n-1)
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console