我的提示代码不起作用

时间:2017-06-13 10:27:18

标签: javascript html

我正在制作一个简单的JS计算器。但是,变量内的提示不会出现。是的,我将把子函数更改为变量,以使switch语句成为可能。但即使将它打入JS修复器后,它也无法工作。任何人都可以告诉我该怎么做?我有点被困在这里。我已经尝试将它放入JSFiddle,但它仍然没有奏效。谢谢,如果我得到答案!

var firstNumber = prompt("Type in the first number that you want to add,subtract,divide,multiply,power(square or to the power of) or square root.");

    var operationForNumber() = prompt("Type in the symbol that matches the operation that you want to use(2 for square, ! for square root, /,for division and a * for multiplication).");

    var secondNumber() = prompt("Type in the second number that you want to add,subtract,divide,multiply,square or square root.");


    switch (operationForNumber) {
        case +:
            firstNumber "+"
            secondNumber
            break;
        case -:
            firstNumber "-"
            secondNumber
            break;
        case /:
        firstNumber "/"
        secondNumber
        case *:
            firstNumber "*"
            secondNumber
            break;
        case 2:
            Math.pow(firstNumber, secondNumber);
            break;
        case !:
            sqrt(firstNumber)
        default:
            console.log("The number you have typed is too great to be calculated or the operation that you chose(+,/,-,X) isn't valid!");
    }

2 个答案:

答案 0 :(得分:1)

您的代码失败,因为您在交换机案例中没有围绕运营商的引号。对所有情况做这样的事情:

case "+":
case "-":

答案 1 :(得分:0)

您必须将提示调用的结果存储在变量中,以使它们可用于交换机内的语句。

var firstNumber = prompt("Type in the first number that you want to add,subtract,divide,multiply,power(square or to the power of) or square root."); var operationForNumber = prompt("Type in the symbol that matches the operation that you want to use(2 for square, ! for square root, /,for division and a * for multiplication)."); var secondNumber = prompt("Type in the second number that you want to add, subtract, divide, multiply, square or square root."); 另请遵循一致的代码样式并使用分号终止语句。