简体   繁体   English

JavaScript 收到结果后添加额外的动作

[英]JavaScript adding additional actions after receiving the result

I'm making a calculator in JS that displays commands and the result in a prompt.我正在用 JS 制作一个计算器,它会在提示中显示命令和结果。

I want to add an action that, after receiving the result of the operation, makes it possible to multiply, divide, etc. by another number.我想添加一个动作,在接收到运算结果后,可以用另一个数字进行乘法、除法等操作。

Action plan: First action... = result > choose selector ( +, -, *,/) > choose second number > result --- and so on until you stop pressing enter with a blank field.行动计划:第一个行动... = 结果 > 选择选择器(+、-、*、/)> 选择第二个数字 > 结果 --- 等等,直到你停止按输入空白字段。

I was thinking of starting with a while loop to add the ability to choose a character but I don't know if it will affect the result of the previous action, additionally I don't know how to add the ability to choose the next number我想从一个 while 循环开始添加选择一个字符的能力,但我不知道它是否会影响前一个动作的结果,另外我不知道如何添加选择下一个数字的能力

    switch (operator) {
        case '+':
            alert(numb1 + numb2)
            break
        case '-':
            alert(numb1 - numb2)
            break
        case '*':
            alert(numb1 * numb2)
            break
        case '/':
            alert(numb1 / numb2)
            break
        case '%':
            alert(numb1 % numb2)
            break
    }
    while (true) {
        let result = +prompt('Enter an arithmetic operator or leave blank.')

        if (!result) break
    }
}

Yes you can use while loop with a new param to get the new value input with prompt, such that:是的,您可以使用带有新参数的 while 循环来获取带有提示的新值输入,这样:

    while (true) {
        let result = +prompt('Enter an arithmetic operator or leave blank.')

        if (!result) break
        let newNumber = +prompt('Enter a new number')
        // do the arithmetic operation
    }

Additionally, the operation in switch seems to be quite redundant.此外,switch 中的操作似乎是相当多余的。 ie: one case for alert in one character / operator change.即:一个字符/操作员更改的警报case You might want to use eval() for the operation.您可能希望使用eval()进行操作。

    let numb1 = 1 // original number
    while (true) {
        let result = prompt('Enter an arithmetic operator or leave blank.')

        if (!result) break
        let newNumber = prompt('Enter a new number')
        // do the arithmetic operation
        // if numb1 = 1, new Number = 2, result = '+', it will show '1 + 2'
        alert(`${numb1} ${result} ${newNumber}`)

        // numb1 = 3
        numb1 = eval(`${numb1} ${result} ${newNumber}`) 


    }

For converting string to operation, please refer to this answer: https://stackoverflow.com/a/26551015/9067107字符串转操作请参考这个答案: https://stackoverflow.com/a/26551015/9067107

This seens like a learning exercise, so I'll try to explain some thought before showing some code.这看起来像是一个学习练习,所以我会在展示一些代码之前尝试解释一些想法。

Your switch case assumes num1 and num2 are already known, but what you described says that num2 will only come after the operator.您的switch盒假设num1num2已知,但您所描述的是num2只会出现在运算符之后。 Also, you want that math being done inside the loop, else it will only run once.此外,您希望在循环内完成数学运算,否则它只会运行一次。

What you want is:你想要的是:

  • type first number;输入第一个数字;
  • type operator;类型运算符;
  • type next number;输入下一个数字;
  • show result = first/old number (operator) next/last number input; show result = first/old number (operator) 下一个/最后一个数字输入;
  • type operator;类型运算符;
  • type next number;输入下一个数字;
  • show result = first/old number (operator) next/last number input; show result = first/old number (operator) 下一个/最后一个数字输入;

... and so on until the "end command" is input. ... 依此类推,直到输入“结束命令”。

We have to respect that order, so we'll need to store results and actions.我们必须遵守该顺序,因此我们需要存储结果和操作。 This way we can keep the loop "alive".这样我们就可以保持循环“活着”。

Another thing, to be a "good loop", it has to start and end with the same actions.另一件事,要成为一个“好的循环”,它必须以相同的动作开始和结束。 So, we'll leave the first number input out of the loop, that way it will be something liek this:所以,我们将把第一个数字输入留在循环之外,这样它就会是这样的:

  1. ask "first number"问“第一个号码”
  2. ask "operator" -> (end?) -> ask "next number" -> store and show "result"询问“操作员”->(结束?)->询问“下一个号码”->存储并显示“结果”
  3. ask "operator" -> (end?) -> ask "next number" -> store and show "result"询问“操作员”->(结束?)->询问“下一个号码”->存储并显示“结果”

... and so on... ... 等等...

// start prompting the user for the first number.
let oldNum = +prompt("first number: ");
let continue = true; // this is the condition to continue or end our loop.

while(continue) {
  // prompt the user for the operator
  let op = prompt('operator: ');
  if (!(op === "+" || op === "-" || op === "/" || op === "*")) break;
  // if anything aside from the expected inputs, the loop breaks.

  let num = prompt('next number: ');
  switch(op) {
    case '+':
      oldNum += num; // we add the new value to the old one.
      break;
    case '-':
      oldNum -= num;
      break;
    case '/':
      oldNum /= num;
      break;
    case '*':
      oldNum *= num;
      break;
  }
  // show result to user
  alert(oldNum);
}

BTW, there are better ways to write this particular code, tried to make it similar to what you shown.顺便说一句,有更好的方法来编写这个特定的代码,试图让它与你展示的相似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM