Javascript计算器使用if / else语句

问题描述 投票:3回答:2

我有一个学校项目要求我使用if / else语句构建一个基本的Javascript计算器。我对这门语言(以及一般的编程)非常陌生,并且在应用我学到的语法以及如何成功解决问题方面遇到了麻烦。

所以代码:正如您将在HTML文件的注释中看到的,所有javascript代码必须在js文件中。首先,我需要通过添加代码来绑定onclick事件,以将所有输入元素放入一个名为inputs(我已经完成)的数组中,然后使用for循环迭代数组(我已经开始)。然后,在for循环中,我需要使用if / else语句跳过不是按钮的输入元素,并将其他输入元素的onclick处理程序设置为调用其中的calcu变量的函数。这是我陷入如何完成这项任务的地方。

我还需要确保将输入[i] .id传递给计算器。当然,我必须使用if / else语句完成calcu()函数。

在js文件中,我插入了注释以显示我的思考过程和问题,我已经让自己解决了这个问题。

我非常感谢你能给我的任何帮助或指导。谢谢!

var calcu = function(calcValue) {

  /* "calc.output.value += "1";"" use to output numbers? */

  if (calcValue) {


  }
};

document.getElementById(inputs[i].id).onclick = calcu;
for (var i = 0; i <= inputs[id].length, i++) {
  /* input button elements onlick handler needs to be set to (equal to?) a
       function that calls the calcu variable inside of it */
  if (input.type = button) {

    console.log()

    /* Need input that is not a button to be skipped*/
  } else(input.type != button) {

  }

}
<body>
  <div id="content">
    <div id="calculator-container">
      <!-- DO NOT CHANGE THE FORM! Only use external JavaScript files -->
      <form name="calc">
        <label for="output">A Basic Calculator</label>
        <!-- the name of the textbox is "output" -->
        <input id="output" name="output" type="text" readonly>

        <br>
        <input type="button" id="1" value="  1  ">
        <input type="button" id="2" value="  2  ">
        <input type="button" id="3" value="  3  ">
        <input type="button" id="+" value="  +  ">
        <br>
        <input type="button" id="4" value="  4  ">
        <input type="button" id="5" value="  5  ">
        <input type="button" id="6" value="  6  ">
        <input type="button" id="-" value="  -  ">
        <br>
        <input type="button" id="7" value="  7  ">
        <input type="button" id="8" value="  8  ">
        <input type="button" id="9" value="  9  ">
        <input type="button" id="*" value="  x  ">
        <br>
        <input type="button" id="c" value="  C  ">
        <input type="button" id="0" value="  0  ">
        <input type="button" id="equate" value="  =  ">
        <input type="button" id="/" value="&divide;">
      </form>
    </div>

  </div>

</body>
javascript if-statement calculator calc
2个回答
2
投票

我发现了一些基本的错误;或者=如果你想把条件放在else部分,那么你也必须在条件下使用else。你需要现在传递输入值for循环正确。

var calcu = function(calcValue) {

  if (calcValue) {


  }
}

document.getElementById(inputs[i].id).onclick = calcu;
for (var i = 0; i <= inputs[id].length; i++) 
{
  if (input.type == button) 
  {
     console.log();
  } 
  else if(input.type != button) {

  }

}

这是你的HTML代码。

<body>
  <div id="content">
    <div id="calculator-container">
      <!-- DO NOT CHANGE THE FORM! Only use external JavaScript files -->
      <form name="calc">
        <label for="output">A Basic Calculator</label>
        <!-- the name of the textbox is "output" -->
        <input id="output" name="output" type="text" readonly>

        <br>
        <input type="button" id="1" value="  1  ">
        <input type="button" id="2" value="  2  ">
        <input type="button" id="3" value="  3  ">
        <input type="button" id="+" value="  +  ">
        <br>
        <input type="button" id="4" value="  4  ">
        <input type="button" id="5" value="  5  ">
        <input type="button" id="6" value="  6  ">
        <input type="button" id="-" value="  -  ">
        <br>
        <input type="button" id="7" value="  7  ">
        <input type="button" id="8" value="  8  ">
        <input type="button" id="9" value="  9  ">
        <input type="button" id="*" value="  x  ">
        <br>
        <input type="button" id="c" value="  C  ">
        <input type="button" id="0" value="  0  ">
        <input type="button" id="equate" value="  =  ">
        <input type="button" id="/" value="&divide;">
      </form>
    </div>

  </div>

</body>

希望这会帮助你。


2
投票

好吧,我想这是你学校的“家庭作业”。

我假设这个计算器应该在加法和减法之前执行乘法和除法。

如果我是你,我将按以下顺序处理任务:

  1. 将用户输入存储到inputs
  2. 当用户按下=时,它会迭代inputs并执行计算。
  3. 执行计算时,声明一个临时变量,在每次计算后保留更新的值。如果指示,您应该在加法和减法之前执行乘法和除法。
  4. 输出计算值

你可能会陷入困境:

  1. 您需要将用户输入转换为正确的数字。例如,3, 5, 1的输入需要转换为351
  2. 在迭代inputs数组时,你需要找到一种在加法和减法之前执行乘法和除法的正确方法。

快乐的编码〜

© www.soinside.com 2019 - 2024. All rights reserved.