首页 > 解决方案 > Ans key 有效,但不能用它进行计算

问题描述

ans密钥应该采用最后输入的数字(不是计算)。这可行,但是当您尝试使用任何计算进行此操作时,ans密钥不起作用。我正在使用全局变量ans并在每个函数之后更新它的值。ans本质上充当,当0它应该充当其文本的值时。这是我的代码:

$(document).ready(function() {
  var result = 0;
  var prevEntry = 0;
  var operation = null;
  var currentEntry = '0';
  var nextEntry = '0';
  var memoryRegister = [];
  var lastEntry = 0;
  var ans;
  var display = document.getElementById("display");
  updateScreen(result);
function radioValue() {
var trig = document.getElementsByName('type');
for (i=0; i<trig.length; i++){
    if(trig[i].checked)
    var trig2 = trig[i].value;
}}

  var displayNum = function(number) {
  if (is_float(number) && number.toString().length > 9) {
    $('.screen').text(number.toPrecision(9).toString());
    return;
}
$('.screen').text(number.toString());
}

var memorySave = function() {
  var num = Number($('.screen').text());
  if (Number.isNaN(num)) return;
  memoryRegister.push(num);

  $('.memory-list').html('');
  memoryRegister.forEach(function(element) {
    $('.memory-list').append('<li>' + element + '</li>')
  });
}

var memoryRecall = function() {
  $('.memory-list').toggle("fast", function(){});
}

var memoryClear = function() {
  memoryRegister = [];
  $('.memory-list').hide("fast", function(){});
  $('.memory-list').html('');
}

var memoryList = function() {
  $('.screen').text($(this).text());
}


$('#memory-save').click(memorySave)
$('#memory-recall').click(memoryRecall)
$('#memory-clear').click(memoryClear)
$('.memory-list').on('click', 'li', memoryList);


  $('.button').on('click', function(evt) {
    var buttonPressed = $(this).html();
    console.log(buttonPressed);
    if (buttonPressed === "C") {
      result = 0;
      currentEntry = '0';
    } else if (buttonPressed === "AC") {
      location.reload(true);
    } else if (buttonPressed === "back") {
      currentEntry = currentEntry.substring(0, currentEntry.length-1);
    } else if (buttonPressed === "+/-") {
      currentEntry *= -1;
    } else if (buttonPressed === '.') {
      currentEntry += '.';
    } else if (buttonPressed === '(') {
      currentEntry = '(';
    } else if (buttonPressed === ')') {
      currentEntry += ')';
    } else if (buttonPressed === 'e<sup>x</sup>') {
      currentEntry = Math.E ** currentEntry;
    } else if (buttonPressed === 'e') {
      currentEntry = '2.7182818284590452353602874713527';
    } else if (isNumber(buttonPressed)) {
      if (currentEntry === '0') currentEntry = buttonPressed; 
      else currentEntry = currentEntry + buttonPressed; ans = currentEntry;
    } else if (isOperator(buttonPressed)) {
      prevEntry = parseFloat(currentEntry);
      operation = buttonPressed;
      currentEntry = '';
    } else if (isOperator(buttonPressed)) {
      lastEntry = parseFloat(currentEntry);
      operation = buttonPressed;
      currentEntry = '';
    } else if (isOperator(buttonPressed)) {
      nextEntry = parseFloat(currentEntry);
      operation = buttonPressed;
      currentEntry = '';
    } else if(buttonPressed === '%') {
      currentEntry = currentEntry / 100;
    } else if(buttonPressed === 'log') {
      currentEntry = Math.log10(currentEntry);
    } else if(buttonPressed === 'ln') {
      currentEntry = Math.log(currentEntry);
    } else if (buttonPressed === '√x') {
      currentEntry = Math.sqrt(currentEntry);
    } else if (buttonPressed === '3√x') {
      currentEntry = Math.cbrt(currentEntry);
    } else if (buttonPressed === '1/x') {
      currentEntry = 1 / currentEntry;
    } else if (buttonPressed === 'RND') {
      currentEntry = Math.random();
    } else if (buttonPressed === 'EXP') {
      currentEntry = Math.pow(10,currentEntry);
    } else if (buttonPressed === 'pi') {
      currentEntry = Math.PI;
    }  else if (buttonPressed === 'x<sup>1/y</sup>') {
      currentEntry = Math.pow(x,y);
    }  else if (buttonPressed === 'sin') {
      prevEntry = parseFloat(currentEntry);
            if ($("#radcheck").is(":checked")) {
                    operation = 'sin';
                    currentEntry = '';
                }
                if ($("#degcheck").is(":checked")) {
                    operation = 'sin1';
                    currentEntry = '';
                }

    } else if (buttonPressed === 'cos') {
      prevEntry = parseFloat(currentEntry);
            if ($("#radcheck").is(":checked")) {
                    operation = 'cos';
                    currentEntry = '';
                }
                if ($("#degcheck").is(":checked")) {
                    operation = 'cos1';
                    currentEntry = '';
                }
    } else if (buttonPressed === 'tan') {
      prevEntry = parseFloat(currentEntry);
            if ($("#radcheck").is(":checked")) {
                    operation = 'tan';
                    currentEntry = '';
                }
                if ($("#degcheck").is(":checked")) {
                    operation = 'tan<sup>1</sup>';
                    currentEntry = '';
                }
    } else if (buttonPressed === 'sin<sup>-1</sup>') {
      prevEntry = parseFloat(currentEntry);
            if ($("#radcheck").is(":checked")) {
                    operation = 'sin<sup>-1</sup>';
                    currentEntry = '';
                }
                if ($("#degcheck").is(":checked")) {
                    operation = 'sin-2';
                    currentEntry = '';
                }
    } else if (buttonPressed === 'cos<sup>-1</sup>') {
      prevEntry = parseFloat(currentEntry);
            if ($("#radcheck").is(":checked")) {
                    operation = 'cos<sup>-1</sup>';
                    currentEntry = '';
                }
                if ($("#degcheck").is(":checked")) {
                    operation = 'cos-2';
                    currentEntry = '';
                }  
    } else if (buttonPressed === 'tan<sup>-1</sup>') {
      prevEntry = parseFloat(currentEntry);
            if ($("#radcheck").is(":checked")) {
                    operation = 'tan<sup>-1</sup>';
                    currentEntry = '';
                }
                if ($("#degcheck").is(":checked")) {
                    operation = 'tan-2';
                    currentEntry = '';
                }  
    } 
     else if (buttonPressed === 'x<sup>2</sup>') {
      currentEntry = Math.pow(currentEntry,2);
    } else if (buttonPressed === 'x<sup>3</sup>') {
      currentEntry = Math.pow(currentEntry,3);
    } else if (buttonPressed === 'x^y') {
     prevEntry = parseFloat(currentEntry);
    operation = 'x^y';
    currentEntry = '';
    } else if (buttonPressed === 'y√x') {
     prevEntry = parseFloat(currentEntry);
    operation = 'y√x';
    currentEntry = '';
    } else if (buttonPressed === '10<sup>x</sup>') {
      currentEntry = Math.pow(10,currentEntry);
    } else if (buttonPressed === 'Ans') {
      currentEntry = ans;
      operation = null;
    } else if (buttonPressed === '=') {
      currentEntry = operate(prevEntry, currentEntry, operation);
      operation = null;
    } 
      else if (buttonPressed === 'n!'){
        var i;
    currentEntry = (currentEntry * currentEntry[currentEntry-1] )
      }
    updateScreen(currentEntry);
  });
});

updateScreen = function(displayValue) {
  var displayValue = displayValue.toString();
  $('.screen').html(displayValue.substring(0, 10));
};

isNumber = function(value) {
  return !isNaN(value);
}

isOperator = function(value) {
  return value === '/' || value === '*' || value === '+' || value === '-';
};

operate = function(a, b, operation) {
  a = parseFloat(a);
  b = parseFloat(b);
  console.log(a, b, operation);
  if (operation === '+') return a + b;
  if (operation === '-') return a - b;
  if (operation === '*') return a * b;
  if (operation === '/') return a / b;
  if (operation === '**') return a * (10**b);
  if (operation === "x^y") return a ** b;
  if (operation === 'y√x') return Math.pow(a,1/b);

  if (operation === 'sin') return Math.sin(a);
  if (operation === 'cos') return Math.cos(a);
  if (operation === 'tan') return Math.tan(a);

  if (operation === 'sin<sup>-1</sup>') return Math.asin(a);
  if (operation === 'cos<sup>-1</sup>') return Math.acos(a);
  if (operation === 'tan<sup>-1</sup>') return Math.atan(a);

  if (operation === 'sin1') return Math.sin(a * Math.PI / 180);
  if (operation === 'cos1') return Math.cos(a * Math.PI / 180);
  if (operation === 'tan1') return Math.tan(a * Math.PI / 180);

  if (operation === 'sin-2') return Math.asin(a) * (180/Math.PI);
  if (operation === 'cos-2') return Math.acos(a) * (180/Math.PI);
  if (operation === 'tan-2') return Math.atan(a) * (180/Math.PI);
}

$( "#key" ).on( "keydown", function( event ) {
  $( "#log" ).html( event.key );
});
body {
  background-color: lightblue;
}

p {
  margin: 0;
  font-family: monospace;
  position: relative;
  left: 5%;
}

.calculator {
  position: relative;
  margin: 1em auto;
  padding: 1em 0;
  display: block-inline;
  width: 450px;
  background-color: #444;
  border-radius: 25px;
  box-shadow: 5px 5px 15px 3px #111;
  font-family: monospace;
}

.calc-row {
  text-align: center;
}

.calc-row div.screen {
  font-family: monospace;
  display: table;
  width: 85%;
  background-color: #aaa;
  text-align: right;
  font-size: 2em;
  min-height: 1.2em;
  margin-left: 0.5em;
  padding-right: 0.5em;
  border: 1px solid #888;
  color: #333;
}

.calc-row div {
  text-align: center;
  display: inline-block;
  font-weight: bold;
  border: 1px solid #555;
  background-color: #eee;
  padding: 10px 0;
  margin: 7px 5px;
  border-radius: 15px;
  box-shadow: 2px 2px 1px 1px #222;
  width: 50px;
}

.calc-row div.zero {
  width: 112px;
}

.calc-row div.zero {
  margin-right: 5px;
}

.screen {
  font-family: 'VT323', monospace;
  font-size: 2.2em;
  text-align: right;
  background-color: #88937f;
  padding: 5px 5px;
  margin-bottom: 10px;
  box-shadow: inset 0px 0px 6px 3px rgba(0, 0, 0, 0.4);
  min-height: 36px;
}
.functions {
  float: right;
  padding-right: 0px;
  text-align: right;
}

.functions {
  position: relative;
}

.memory-list {
  display: none;
  position: absolute;
  left: 31px;
  top: 18px;
  min-width: 42px;
  border: 1px solid #226073;
  padding: 5px 0;
  margin: 0;
  list-style-type: none;
  background-color: #448096;
  color: #fff;
  box-shadow: 1px 3px 6px rgba(0, 0, 0, 0.8);
}

.memory-list li {
  padding: 3px 10px;
  text-align: left;
}

.memory-list li:hover {
  background-color: #226073;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<p>Unit Four Project Tyler Glynn</p>
<input id="key" value="">
<div id="log"></div>
<div class="calculator">

    <div class="calc-row">
        <input type="hidden" id="operation" value="" />
        <div id="display" class="screen">0123456789</div>
    </div>

    <div class="calc-row">
        <input type="radio" name="type" value="degree" id="degcheck">Deg
        <input type="radio" name="type" value="radian" id='radcheck'>Rad
        <div class="button">C</div>
        <div class="button">AC</div>
        <div class="button backspace">back</div>
        <div class="button">(</div>
        <div class="button">)</div>
        <div class="button root">3√x</div>
        <div class="button" id="memory-save">M+ </div>
        <div class="button" id="memory-recall">MR &#9660;</div>
        <ul class="memory-list"></ul>
        <div class="button" id="memory-clear">M-</div>
        <div class="button">log</div>
        <div class="button">ln</div>
    </div>

    <div class="calc-row">
        <div id="7" value="7" class="button">7</div>
        <div id="8" value="8" class="button">8</div>
        <div id="9" value="9" class="button">9</div>
        <div class="button divice">/</div>
        <div class="button root">√x</div>
        <div class="button">10<sup>x</sup></div>
        <div class="button plus-minus">+/-</div>
    </div>

    <div class="calc-row">
        <div id="4" class="button">4</div>
        <div id="5" class="button">5</div>
        <div id="6" class="button">6</div>
        <div class="button multiply">*</div>
        <div class="button inverse">1/x</div>
        <div class="button">EXP</div>
        <div class="button">%</div>
    </div>

    <div class="calc-row">
        <div id="1" value="1" class="button">1</div>
        <div id="2" value="2" class="button">2</div>
        <div id="3" value="3" class="button">3</div>
        <div class="button">-</div>
        <div class="button pi">pi</div>
        <div class="button">log</div>
        <div class="button">RND</div>
    </div>

    <div class="calc-row">
        <div id="0" value="0" class="button zero">0</div>
        <div class="button decimal" id="whole">.</div>
        <div class="button">+</div>
        <div id="equals" value="=" class="button equal">=</div>
        <div class="button">n!</div>
    </div>
    <div class="calc-row">
        <div class="button">sin</div>
        <div class="button">cos</div>
        <div class="button">tan</div>
        <div class="button">sin<sup>-1</sup></div>
        <div class="button">cos<sup>-1</sup></div>
        <div class="button">tan<sup>-1</sup></div>
        <div class="button">Ans</div>
        <div class="button multiply">x^y</div>
        <div class="button">x<sup>2</sup></div>
        <div class="button">x<sup>3</sup></div>
        <div class="button">e</div>
        <div class="button">e<sup>x</sup></div>
        <div class="button">y√x</div>
    </div>
</div>

<table>

</table>

标签: jquery

解决方案


推荐阅读