在JS

时间:2017-10-10 09:12:01

标签: javascript

我是一个完全的初学者,已经开始通过免费代码阵营学习javascript。我正在尝试使用基本的javascript编写程序来制作计算器。我的计算器似乎工作正常,除了它有几个我想解决的问题。编辑以澄清我在这里寻求帮助的内容。请通过以下操作在下面的代码段中运行我的代码,您将了解我的要求。键入9-2然后点击'='计算器应显示'7'。如果在显示“7”后立即键入“2”,它将在屏幕上显示“72”,这是我不希望它做的。我希望它重置为空白屏幕。我的另一个问题是重复显示运算符。我使用过其他评论者提供的代码,但它们似乎不起作用。我会继续寻找解决方案。谢谢大家。

  1. 当重复按下按钮时,此时可以重复运算符+-*/.。如何创建一个仅允许它显示一次的功能。
  2. 点击“相等”按钮后,屏幕上显示结果。如果我按下另一个数字,它将添加到结果而不是在新屏幕上开始。我需要它使用我当前的代码重置并启动一个新的。 任何指导和方向表示赞赏。我已经搜索谷歌几天了,仍然坚持。我在逻辑方面也很糟糕,我正在尽力绕过这个。谢谢。
  3. var show = document.getElementById('display');
    var reset = false;
    
    function clear() {
      if (reset) {
        show.value = '';
        reset = false;
      }
    }
    
    function toScreen(x) {
      clear();
      show.value += x;
      if (x === 'c') {
        show.value = '';
      }
    }
    
    function answer() {
      x = show.value;
      x = eval(x);
      show.value = x;
      reset = false;
    }
    
    function power() {
      x = show.value;
      x = eval(x * x);
      show.value = x;
    }
    
    function backspace() {
      var num = show.value;
      var len = num.length - 1;
      var newNum = num.substring(0, len);
      show.value = newNum;
    }
    
    function percent() {
      x = show.value;
      x = eval(x / 100);
      show.value = x;
    }
    
    function opposite() {
      var n = show.value;
      n = n * -1;
      show.value = n;
    }
    
    function sqrt() {
      var number = show.value;
      var ans = Math.sqrt(number);
      if (number < 0)
        ans = "Try a positive number!";
      show.value = ans;
    }
    
    function pie() {
      var pi = show.value;
      var result = Math.PI;
      show.value = result;
    }
    <form>
      <input type="text" id="display" disabled><br>
    
      <input type="button" value="C" id="keys" onclick="toScreen('c')">
      <input type="button" value="DEL" id="keys" onclick="backspace()">
      <input type="button" value="π" id="keys" onclick="pie()">
      <input type="button" value="+/-" id="keys" onclick="opposite()"><br>
    
      <input type="button" value="√" id="keys" onclick="sqrt()">
      <input type="button" value="%" id="keys" onclick="percent()">
      <input type="button" value="x^2" id="keys" onclick="power()">
      <input type="button" value="+" id="keys" onclick="toScreen('+')"><br>
    
      <input type="button" value="9" id="keys" onclick="toScreen('9')">
      <input type="button" value="8" id="keys" onclick="toScreen('8')">
      <input type="button" value="7" id="keys" onclick="toScreen('7')">
      <input type="button" value="-" id="keys" onclick="toScreen('-')"><br>
    
      <input type="button" value="6" id="keys" onclick="toScreen('6')">
      <input type="button" value="5" id="keys" onclick="toScreen('5')">
      <input type="button" value="4" id="keys" onclick="toScreen('4')">
      <input type="button" value="*" id="keys" onclick="toScreen('*')"><br>
    
      <input type="button" value="3" id="keys" onclick="toScreen('3')">
      <input type="button" value="2" id="keys" onclick="toScreen('2')">
      <input type="button" value="1" id="keys" onclick="toScreen('1')">
      <input type="button" value="/" id="keys" onclick="toScreen('/')"><br>
    
      <input type="button" value="0" id="keys" onclick="toScreen('0')">
      <input type="button" value="." id="keys" onclick="toScreen('.')">
      <input type="button" value="=" id="equal" onclick="answer()"><br>
    </form>

3 个答案:

答案 0 :(得分:1)

查看代码

这是完美的代码。您可以在某些地方替换x,例如:

function power() {
  x = eval(i * i);
  show.value = x;
  reset = true;
}

问题已解决:

  1. 你的第一个&amp;第二个问题

  2. 您的代码没有检查是否从num开始输入。或运算符(/ 1,* 1)

  3. 您的代码没有检查无效输入(1 +,1 + 1 +等)

  4. 您的代码在电源Pi等之后也无法清除屏幕(如果是anser)

  5. 您的功能如PI Power不检查输入是否为数字(功率为8 *)请参阅电源功能:

  6. &#13;
    &#13;
    var show = document.getElementById('display');
    var reset = false;
    var i = "";  //store what is typed
    
    function clear() {
      if (reset) {
        i = '';
        show.value = i;
        reset = false;
      }
    }
    
    function toScreen(x) {
      clear();
      if (x === "+" || x === "-" || x === "*" || x === "/" || x === ".") {
        if (i.charAt(i.length - 1)  === x) {
          return false;
        }
        if (i.length === 0) {
          return false;
        }
      }
      i += x;
      if (x === 'c') {
        i = '';
      }
      show.value = i;
    }
    
    function answer() {
      var op = ["+", "-", "*", ".", "/"];
      for (a=0; a<op.length; a++) {
        if (i.charAt(i.length - 1) === op[a]) {
          alert("Wrong input");
          return false;
        }
      }
      x = show.value;
      x = eval(x);
      show.value = x;
      reset = true;
    }
    
    function power() {
      if (isNaN(i)) {
         alert("please enter a valid number");
         return false;
       }
      x = show.value;
      x = eval(x * x);
      show.value = x;
      reset = true;
    }
    
    function backspace() {
      var num = show.value;
      var len = num.length - 1;
      var newNum = num.substring(0, len);
      show.value = newNum;
      reset = true;
    }
    
    function percent() {
      x = show.value;
      x = eval(x / 100);
      show.value = x;
      reset = true;
    }
    
    function opposite() {
      var n = show.value;
      n = n * -1;
      show.value = n;
      reset = true;
    }
    
    function sqrt() {
      var number = show.value;
      var ans = Math.sqrt(number);
      if (number < 0)
        ans = "Try a positive number!";
      show.value = ans;
      reset = true;
    }
    
    function pie() {
      var pi = show.value;
      var result = Math.PI;
      show.value = result;
      reset = true;
    }
    &#13;
    <form>
      <input type="text" id="display" disabled><br>
    
      <input type="button" value="C" id="keys" onclick="toScreen('c')">
      <input type="button" value="DEL" id="keys" onclick="backspace()">
      <input type="button" value="π" id="keys" onclick="pie()">
      <input type="button" value="+/-" id="keys" onclick="opposite()"><br>
    
      <input type="button" value="√" id="keys" onclick="sqrt()">
      <input type="button" value="%" id="keys" onclick="percent()">
      <input type="button" value="x^2" id="keys" onclick="power()">
      <input type="button" value="+" id="keys" onclick="toScreen('+')"><br>
    
      <input type="button" value="9" id="keys" onclick="toScreen('9')">
      <input type="button" value="8" id="keys" onclick="toScreen('8')">
      <input type="button" value="7" id="keys" onclick="toScreen('7')">
      <input type="button" value="-" id="keys" onclick="toScreen('-')"><br>
    
      <input type="button" value="6" id="keys" onclick="toScreen('6')">
      <input type="button" value="5" id="keys" onclick="toScreen('5')">
      <input type="button" value="4" id="keys" onclick="toScreen('4')">
      <input type="button" value="*" id="keys" onclick="toScreen('*')"><br>
    
      <input type="button" value="3" id="keys" onclick="toScreen('3')">
      <input type="button" value="2" id="keys" onclick="toScreen('2')">
      <input type="button" value="1" id="keys" onclick="toScreen('1')">
      <input type="button" value="/" id="keys" onclick="toScreen('/')"><br>
    
      <input type="button" value="0" id="keys" onclick="toScreen('0')">
      <input type="button" value="." id="keys" onclick="toScreen('.')">
      <input type="button" value="=" id="equal" onclick="answer()"><br>
    </form>
    &#13;
    &#13;
    &#13;

答案 1 :(得分:0)

function answer() {
  x = show.value;
  x = eval(x);
  show.value = x;
  reset = true;
}

将重置更改为true,以便将show.value清除为空字符串,为您解决nr.2

答案 2 :(得分:0)

  
      
  1. 修改您的toScreen()功能,仅添加operator(如果是)   以前没有输入:
  2.   
function toScreen(x) {
  clear();
  var notTwice = ['+', '-', '*', '/', '.']; //List of chars that should not be repeated
  if(notTwice.indexOf(x) > -1){
    if(show.value.slice(-1) != x){ //Only type it if it wasn't typed right before
      show.value += x;
    }
  }
  else{
    show.value += x;
  }
  if (x === 'c') {
    show.value = '';
  }
}
  
      
  1. 只需在reset函数中将false设置为answer()
  2.   

以下是适合您的工作片段:

var show = document.getElementById('display');
var reset = false;
var i = "";  //store what is typed

function clear() {
  if (reset) {
    i = '';
    show.value = i;
    reset = false;
  }
}

function toScreen(x) {
  clear();
  var notTwice = ['+', '-', '*', '/', '.']; //List of chars that should not be repeated
  if(notTwice.indexOf(x) > -1){
    if(show.value.slice(-1) != x){ //Only type it if it wasn't typed right before
      show.value += x;
    }
  }
  else{
    show.value += x;
  }
  if (x === 'c') {
    show.value = '';
  }
}

function answer() {
  var op = ["+", "-", "*", ".", "/"];
  for (a=0; a<op.length; a++) {
    if (i.charAt(i.length - 1) === op[a]) {
      alert("Wrong input");
      return false;
    }
  }
  x = show.value;
  x = eval(x);
  show.value = x;
  reset = false;
}

function power() {
  if (isNaN(i)) {
     alert("please enter a valid number");
     return false;
   }
  x = show.value;
  x = eval(x * x);
  show.value = x;
  reset = true;
}

function backspace() {
  var num = show.value;
  var len = num.length - 1;
  var newNum = num.substring(0, len);
  show.value = newNum;
  reset = true;
}

function percent() {
  x = show.value;
  x = eval(x / 100);
  show.value = x;
  reset = true;
}

function opposite() {
  var n = show.value;
  n = n * -1;
  show.value = n;
  reset = true;
}

function sqrt() {
  var number = show.value;
  var ans = Math.sqrt(number);
  if (number < 0)
    ans = "Try a positive number!";
  show.value = ans;
  reset = true;
}

function pie() {
  var pi = show.value;
  var result = Math.PI;
  show.value = result;
  reset = true;
}
<form>
  <input type="text" id="display" disabled><br>

  <input type="button" value="C" id="keys" onclick="toScreen('c')">
  <input type="button" value="DEL" id="keys" onclick="backspace()">
  <input type="button" value="π" id="keys" onclick="pie()">
  <input type="button" value="+/-" id="keys" onclick="opposite()"><br>

  <input type="button" value="√" id="keys" onclick="sqrt()">
  <input type="button" value="%" id="keys" onclick="percent()">
  <input type="button" value="x^2" id="keys" onclick="power()">
  <input type="button" value="+" id="keys" onclick="toScreen('+')"><br>

  <input type="button" value="9" id="keys" onclick="toScreen('9')">
  <input type="button" value="8" id="keys" onclick="toScreen('8')">
  <input type="button" value="7" id="keys" onclick="toScreen('7')">
  <input type="button" value="-" id="keys" onclick="toScreen('-')"><br>

  <input type="button" value="6" id="keys" onclick="toScreen('6')">
  <input type="button" value="5" id="keys" onclick="toScreen('5')">
  <input type="button" value="4" id="keys" onclick="toScreen('4')">
  <input type="button" value="*" id="keys" onclick="toScreen('*')"><br>

  <input type="button" value="3" id="keys" onclick="toScreen('3')">
  <input type="button" value="2" id="keys" onclick="toScreen('2')">
  <input type="button" value="1" id="keys" onclick="toScreen('1')">
  <input type="button" value="/" id="keys" onclick="toScreen('/')"><br>

  <input type="button" value="0" id="keys" onclick="toScreen('0')">
  <input type="button" value="." id="keys" onclick="toScreen('.')">
  <input type="button" value="=" id="equal" onclick="answer()"><br>
</form>