如何使用 Javascript 函数中的 Prompt、if 和 else 来计算 select 算术符号并根据用户输入进行一些计算

How can I use Prompt, if and else in Javascript function to select arithmetic symbol and do some calculation with the user inputs

我是新手,这真的让我脱发;我可以找到我做错了什么,请帮忙。我在 javascript 中这样做。它不显示任何错误,也不显示任何结果。这是我的:

    var sumIt;
    var subtractIt;
    var multiplyIt;
    var divideIt;
    var operatorOpt = prompt("Select operator");
    
    function showResult(whatResult) {
        document.write(whatResult);
        document.write("<br>");
    }
    
    var doSomething = function(num1, num2) {
        if (operatorOpt == sumIt) {
            showResult("The result is: " + (num1 + num2));
    
    
        } else if (operatorOpt == subtractIt) {
            showResult("The result is: " + (num1 - num2));
    
        }  else if (operatorOpt == multiplyIt) {
            showResult("The result is: " + (num1 * num2));
    
    
    }  else if (operatorOpt == divideIt) {
            showResult("The result is: " + (num1 / num2));
    
    doSomething(parseInt (prompt("Enter first number: ")) ,  parseInt (prompt("Enter second number: ")))

只需在提示符下使用如下选项创建一个列表:

    prompt("Select operator:" 
    + "\n1. Addition" 
    + "\n2.Subtraction" 
    + "\n3.Multiplication" 
    + "\n4.Division"); 

然后将用户提供的号码与操作员号码进行比较。

您似乎在 doSomething 函数的定义中缺少一个右括号。 以下代码似乎可以产生预期的结果

var sumIt = "+";
var subtractIt = "-";
var multiplyIt = "*";
var divideIt = "/";
var operatorOpt = prompt("Select operator");

function showResult(whatResult) {
    console.log(whatResult);
    document.write(whatResult);
    document.write("<br>");
}

var doSomething = function(num1, num2) {
    if (operatorOpt == sumIt) {
        showResult("The result is: " + (num1 + num2));
    } else if (operatorOpt == subtractIt) {
        showResult("The result is: " + (num1 - num2));
    }  else if (operatorOpt == multiplyIt) {
        showResult("The result is: " + (num1 * num2));
    }  else if (operatorOpt == divideIt) {
        showResult("The result is: " + (num1 / num2));
    } else {
      console.log("No Condition reached");
    }

 }

 doSomething(parseInt (prompt("Enter first number: ")) ,  parseInt (prompt("Enter second number: ")));