将数字提升到权力会给我带来麻烦

时间:2017-04-26 19:10:17

标签: jquery calculator

我试图制作它,以便您可以点击按钮,然后点击^,然后点击另一个按钮,它会将第一个数字提升到第二个数字。

代码:

$(document).ready(function() {
  var testNumLength = function(number) {
    if (number.length > 9) {
      totaldiv.text(number.substr(number.length - 9, 9));

    }
  };
  var number = "";
  var newnumber = "";
  var operator = "";
  var totaldiv = $("#total");
  totaldiv.text("0");
  $("#numbers a").not("#clear,#clearall").click(function() {
    number += $(this).text();
    totaldiv.text(number);
    testNumLength(number);
  });
  $("#operators a, #side a").not("#equals, #decimal").click(function() {
    operator = $(this).text();
    newnumber = number;
    number = "";
    totaldiv.text("0");
    if ($(this).attr('id') === "sqrt") {
      operator = "sqrt";
      $("#equals").click();
    }
  });
  $("#clear,#clearall").click(function() {
    number = "";
    totaldiv.text("0");
    if ($(this).attr("id") === "clearall") {
      newnumber = "";
    }
  });
  //Add your last .click() here!
  $("#equals").click(function() {
    if (operator === "+") {
      number = (parseInt(number, 10) + parseInt(newnumber, 10)).toString(10);
    } else if (operator === "-") {
      number = (parseInt(newnumber, 10) - parseInt(number, 10)).toString(10);
    } else if (operator === "÷") {
      number = (parseInt(newnumber, 10) / parseInt(number, 10)).toString(10);
    } else if (operator === "×") {
      number = (parseInt(newnumber, 10) * parseInt(number, 10)).toString(10);
    } else if (operator === "sq") {
      number = (Math.pow(parseInt(newnumber, 10), parseInt(number, 10))).toString(10);
    } else if (operator === "sqrt") {
      number = newnumber;
      number = Math.sqrt(parseFloat(number, 10)).toString(10);
    }
    totaldiv.text(number);
    testNumLength(number);
    number = "";
    newnumber = "";
  });
  $('#decimal').click(function() {
    var numOfDecs = 0;
    for (var i = 0; i < number.length; i++) {
      if (number[i] === ".") {
        totaldiv.text(number[i]);
      }
    }
    if (numOfDecs === 0) {
      number.append('.');
      totaldiv.text(number);
      testNumLengh(number);
    }
  })
});

这是一个小提琴: https://jsfiddle.net/fqdxkL4y/3/

它只会保留第二个数字,而不是显示结果。

1 个答案:

答案 0 :(得分:1)

看起来你把指数按钮的id代替了#34; ^&#34;的文本。

改变这个:

} else if (operator === "sq") {

到此:

} else if (operator === "^") {