【发布时间】:2017-06-13 18:38:57
【问题描述】:
我正在制作一个简单的 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!");
}
【问题讨论】:
-
用引号包裹你的操作符,否则你有语法错误
-
运算符如 +,- 等?
-
你需要在你看到语法错误的环境中运行它,因为它们太多了(缺少变量、不存在的函数等)
-
firstNumber、secondNumber和operationForNumber是函数,需要调用它们才能获取值。他们需要return一些东西,才能返回一些东西。
标签: javascript html