JavaScript if “x = (a || b || c)” statement not working

做~自己de王妃 提交于 2019-11-26 07:49:33

问题


I am making a simple trigonometry program in javascript and my if and while statements are not working properly, as they only pass if the first condition is true i.e. if you type in Sine it will work, but not if you type in Cosine or Tangent.

<script language=\"JavaScript\">
var opposite = 1
var adjacent = 1
var hypotenuse = 1
var sct = \"SohCahToa\"
while (!(sct == (\"Sine\" || \"Cosine\" || \"Tangent\"))) {
    sct = prompt(\"Sine (unknown adjacent) / Cosine (unkown opposite side) / Tangent (unknown hypotenuse)\")
    if (!(sct == (\"Sine\" || \"Cosine\" || \"Tangent\"))) {
        alert(\"Spelling error, please try again\")
    }
}
if (sct == (\"Sine\" || \"Cosine\"))
    hypotenuse = prompt(\"What is the hypotenuse\")
if (sct == (\"Sine\" || \"Tangent\"))
    opposite = prompt(\"What is the opposite side\")
if (sct == (\"Tangent\" || \"Cosine\"))
    adjacent = prompt(\"What is the adjacent side\")

Thanks (save the code as a .html to test it)


回答1:


All your multiple comparisons that look like this:

if (sct == ("Sine" || "Cosine" || "Tangent"))

need to be changed to this:

if (sct == "Sine" || sct == "Cosine" || sct == "Tangent")

To explain, when you do this ("Sine" || "Cosine" || "Tangent"), that evaluates to just "Sine" so if (sct == ("Sine" || "Cosine" || "Tangent")) is the same as if (sct == "Sine") which is obviously not what you want.


Here is your code with all the corrections applied:

var opposite = 1
var adjacent = 1
var hypotenuse = 1
var sct = "SohCahToa"
while (!(sct == "Sine" || sct == "Cosine" || sct == "Tangent")) {
    sct = prompt("Sine (unknown adjacent) / Cosine (unkown opposite side) / Tangent (unknown hypotenuse)")
    (!(sct == "Sine" || sct == "Cosine" || sct == "Tangent")) {
        alert("Spelling error, please try again")
    }
}
if (sct == "Sine" || sct == "Cosine")
    hypotenuse = prompt("What is the hypotenuse")
if (sct == "Sine" || sct == "Tangent")
    opposite = prompt("What is the opposite side")
if (sct == "Tangent" || sct == "Cosine")
    adjacent = prompt("What is the adjacent side")



回答2:


I would use an array for the options, and a case statement below:

var opposite = 1
var adjacent = 1
var hypotenuse = 1
var sct = "SohCahToa"
var options = ["Sine", "Cosine", "Tangent"];
while (options.indexOf(sct) < 0) {
  sct = prompt("Sine (unknown adjacent) / Cosine (unkown opposite side) / Tangent (unknown hypotenuse)");
  sct = options[options.indexOf(sct)];
  if (options.indexOf(sct) < 0) {
    alert("Spelling error, please try again");
  }
}
switch (sct) {
  case "Sine":
    hypotenuse = prompt("What is the hypotenuse")
    opposite = prompt("What is the opposite side")
    break;
  case "Cosine":
    hypotenuse = prompt("What is the hypotenuse")
    adjacent = prompt("What is the adjacent side")
    break;
  default:
    opposite = prompt("What is the opposite side")
    adjacent = prompt("What is the adjacent side")
}


来源:https://stackoverflow.com/questions/30366805/javascript-if-x-a-b-c-statement-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!