Javascript计算器使用if / else语句

时间:2018-05-25 05:59:48

标签: javascript if-statement calculator calc

我有一个学校项目要求我使用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>

2 个答案:

答案 0 :(得分:2)

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

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>

希望这会对你有所帮助。

答案 1 :(得分:2)

嗯,我想这是你学校的'家庭作业'。

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

如果我是你,我会按以下顺序接近任务:

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

  4. 输出计算值

  5. 你可能会陷入困境:

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