多个 if 语句和 then else Javascript

发布于 2025-01-01 17:33:36 字数 3845 浏览 0 评论 0原文

我正在尝试在 javascript 中为反三角函数(反正弦、反余弦、反正切)创建一个计算器,并且我拥有它,因此每个输入都有复选框,因此 window.alert 不会返回 NULL (用户友好)。 形式:

<form name="inverse">
        <legend>Legend is here.</legend>
        <input type="checkbox" name="inverse-cb1" value="sine"></input><label> sin<sup>-1</sup> ( </label><input type="text" size=5 name="sin"><label> )</label><br>
        <input type="checkbox" name="inverse-cb2" value="cosine"></input><label> cos<sup>-1</sup> ( </label><input type="text" size=5 name="cos"><label> )</label><br>
        <input type="checkbox" name="inverse-cb3" value="tangent"></input><label> tan<sup>-1</sup> ( </label><input type="text" size=5 name="tan"><label> )</label><br>
        <br><input type="button" onclick="trigI()" value="Calculate">
    </form>

脚本:

function trigI(){
var sin = document.inverse.sin.value;      //sine
var cos = document.inverse.cos.value;      //cosine
var tan = document.inverse.tan.value;      //tangent

var sin1 = Math.asin(sin);      //arcsine
var cos1 = Math.acos(cos);      //arccosine
var tan1 = Math.atan(tan);      //arctangent

var sin1d = sin1 * (180/Math.PI);    //convert radians to degrees (sine)
var cos1d = cos1 * (180/Math.PI);    //convert radians to degrees (cosine)
var tan1d = tan1 * (180/Math.PI);    //convert radians to degrees (tangent)

if (isNaN(sin) || isNaN(cos) || isNaN(tan) ){
    window.alert("Please input a number.");

    return;
}

if (!document.inverse.inverse-cb1.checked){    //no sine input
    window.alert("When cos(\u2220) = " + cos + " and tan(\u2220) = " + tan + " :" + "\n\n" + "cos\u2212\u00B2(A) = " + cos1d + "\n" + "tan\u2212\u00B2(A) = " + tan1d + "\u00B0");

    return;
}

if (!document.inverse.inverse-cb2.checked){    //no cosine input
    window.alert("When sin(\u2220) = " + sin + " and tan(\u2220) = " + tan + " :" + "\n\n" + "sin\u2212\u00B2(A) = " + sin1d + "\u00B0" + "\n" + "tan\u2212\u00B2(A) = " + tan1d + "\u00B0");

    return;
}

if (!document.inverse.inverse-cb3.checked){    //no tangent input
    window.alert("When sin(\u2220) = " + sin + " and cos(\u2220) = " + cos + " :" + "\n\n" + "sin\u2212\u00B2(A) = " + sin1d + "\u00B0" + "\n" + "cos\u2212\u00B2(A) = " + cos1d + "\u00B0");

    return;
}

if (!document.inverse.inverse-cb1.checked && !document.inverse.inverse-cb2.checked){       //no sine and cosine input
    window.alert("When tan(\u2220) = " + tan + " :" + "\n\n" + "tan\u2212\u00B2(A) = " + tan1d + "\u00B0");

    return;
}

if (!document.inverse.inverse-cb2.checked && !document.inverse.inverse-cb3.checked){       //no cosine and tangent input
    window.alert("When sin(\u2220) = " + sin + " :" + "\n\n" + "sin\u2212\u00B2(A) = " + sin1d + "\u00B0");

    return;
}

if (!document.inverse.inverse-cb1.checked && !document.inverse.inverse-cb3.checked){       //no sine and tangent input
    window.alert("When cos(\u2220) = " + cos + " :" + "\n\n" + "cos\u2212\u00B2(A) = " + cos1d + "\u00B0");

    return;
}

if (!document.inverse.inverse-cb1.checked && !document.inverse.inverse-cb2.checked && !document.inverse.inverse-cb3.checked){     //no input or checked boxes
    window.alert("Please input a number or check the correct boxes.");

    return;
}

else {
    window.alert("When sin(\u2220) = " + sin + ", cos(\u2220) = " + cos + ", and tan(\u2220) = " + tan + " :" + "\n\n" + "sin\u2212\u00B2(A) = " + sin1d + "\u00B0" + "\n" + "cos\u2212\u00B2(A) = " + cos1d + "\u00B0" + "\n" + "tan\u2212\u00B2(A) = " + tan1d + "\u00B0");
    return;
}

}

问题: 多个 if 语句不起作用,因此该函数永远不会返回。如何检查输入条件并以正确的方式响应?

I'm trying to create a calculator in javascript for inverse trigonometry functions (arcsine, arccosine, arctangent) and I have it so there are checkboxes for each input so window.alert doesn't return NULL (user-friendly).
The Form:

<form name="inverse">
        <legend>Legend is here.</legend>
        <input type="checkbox" name="inverse-cb1" value="sine"></input><label> sin<sup>-1</sup> ( </label><input type="text" size=5 name="sin"><label> )</label><br>
        <input type="checkbox" name="inverse-cb2" value="cosine"></input><label> cos<sup>-1</sup> ( </label><input type="text" size=5 name="cos"><label> )</label><br>
        <input type="checkbox" name="inverse-cb3" value="tangent"></input><label> tan<sup>-1</sup> ( </label><input type="text" size=5 name="tan"><label> )</label><br>
        <br><input type="button" onclick="trigI()" value="Calculate">
    </form>

The script:

function trigI(){
var sin = document.inverse.sin.value;      //sine
var cos = document.inverse.cos.value;      //cosine
var tan = document.inverse.tan.value;      //tangent

var sin1 = Math.asin(sin);      //arcsine
var cos1 = Math.acos(cos);      //arccosine
var tan1 = Math.atan(tan);      //arctangent

var sin1d = sin1 * (180/Math.PI);    //convert radians to degrees (sine)
var cos1d = cos1 * (180/Math.PI);    //convert radians to degrees (cosine)
var tan1d = tan1 * (180/Math.PI);    //convert radians to degrees (tangent)

if (isNaN(sin) || isNaN(cos) || isNaN(tan) ){
    window.alert("Please input a number.");

    return;
}

if (!document.inverse.inverse-cb1.checked){    //no sine input
    window.alert("When cos(\u2220) = " + cos + " and tan(\u2220) = " + tan + " :" + "\n\n" + "cos\u2212\u00B2(A) = " + cos1d + "\n" + "tan\u2212\u00B2(A) = " + tan1d + "\u00B0");

    return;
}

if (!document.inverse.inverse-cb2.checked){    //no cosine input
    window.alert("When sin(\u2220) = " + sin + " and tan(\u2220) = " + tan + " :" + "\n\n" + "sin\u2212\u00B2(A) = " + sin1d + "\u00B0" + "\n" + "tan\u2212\u00B2(A) = " + tan1d + "\u00B0");

    return;
}

if (!document.inverse.inverse-cb3.checked){    //no tangent input
    window.alert("When sin(\u2220) = " + sin + " and cos(\u2220) = " + cos + " :" + "\n\n" + "sin\u2212\u00B2(A) = " + sin1d + "\u00B0" + "\n" + "cos\u2212\u00B2(A) = " + cos1d + "\u00B0");

    return;
}

if (!document.inverse.inverse-cb1.checked && !document.inverse.inverse-cb2.checked){       //no sine and cosine input
    window.alert("When tan(\u2220) = " + tan + " :" + "\n\n" + "tan\u2212\u00B2(A) = " + tan1d + "\u00B0");

    return;
}

if (!document.inverse.inverse-cb2.checked && !document.inverse.inverse-cb3.checked){       //no cosine and tangent input
    window.alert("When sin(\u2220) = " + sin + " :" + "\n\n" + "sin\u2212\u00B2(A) = " + sin1d + "\u00B0");

    return;
}

if (!document.inverse.inverse-cb1.checked && !document.inverse.inverse-cb3.checked){       //no sine and tangent input
    window.alert("When cos(\u2220) = " + cos + " :" + "\n\n" + "cos\u2212\u00B2(A) = " + cos1d + "\u00B0");

    return;
}

if (!document.inverse.inverse-cb1.checked && !document.inverse.inverse-cb2.checked && !document.inverse.inverse-cb3.checked){     //no input or checked boxes
    window.alert("Please input a number or check the correct boxes.");

    return;
}

else {
    window.alert("When sin(\u2220) = " + sin + ", cos(\u2220) = " + cos + ", and tan(\u2220) = " + tan + " :" + "\n\n" + "sin\u2212\u00B2(A) = " + sin1d + "\u00B0" + "\n" + "cos\u2212\u00B2(A) = " + cos1d + "\u00B0" + "\n" + "tan\u2212\u00B2(A) = " + tan1d + "\u00B0");
    return;
}

}

The Question: The multiple if statements are not working so the function never returns. How can it be made to check to conditions of the input and respond in the proper way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫九 2025-01-08 17:33:36

我无法告诉你该怎么做才能让它“工作”,因为你想要的结果还不清楚,但是当我从上到下浏览时,你的代码中的许多问题突然出现在我面前。有些仍然会运行,但会给出您意想不到的结果,但至少有一个问题会导致错误并停止函数运行。因此:

您的大多数注释要么是多余的,可以被删除,或者如果您使变量名称更具描述性,则可以变得多余并被删除。

该语句

if (isNaN(document.inverse.sin.value) == true || ...)

可以变得更简单,因为您已经声明了一个等于 document.inverse.sin.value 的变量 sin== true 部分是多余的。只是说,

if (isNan(sin) || isNan(cos) || isNan(tan)) {

您所有的“重置”语句,例如 var sin = null; 都是错误且毫无意义的,因为 (a) 使用 var 意味着您正在尝试重新声明变量(我认为 JS 只是忽略了这一点),所以你应该只说 sin = null;,但无论如何 (b) 它们是 trigI( 中的本地变量) 函数,所以它们都是关于当函数返回时无论如何都会消失。如果您尝试清除屏幕上的字段,则需要说 document.inverse.sin.value = "";,但您不想通过清除所有字段来惹恼用户这些字段只是因为其中一个无效。告诉他们哪个字段有问题,将焦点放在该字段上,然后让他们自己纠正。

大多数 if 语句都会测试如下内容:

if (document.inverse-cb1.checked == 'false'){

这永远不会是 true,因为您正在比较 .checked 属性,该属性是一个等于 truefalse字符串 'false'。你需要说

if (document.inverse-cb1.checked == false){    // note: no quotes
// OR, even better
if (!document.inverse-cb1.checked){

sup.sup() 应该做什么? sup 是一个已设置为 -1 的变量。 sup() 是一个不存在的函数,在任何情况下您都不应该尝试将其作为数字的方法进行调用。 首先解决这个问题,因为我认为这就是阻止函数返回的原因。

编辑:我注意到另一个大问题:

document.inverse.inverse-cb1.checked

对于不遵循 JS 标识符命名规则的属性,不能使用“点”对象表示法,并且 JS 标识符不能有“-” ”在他们之中。因此,虽然“inverse-cb1”是一个有效的属性名称,但您必须使用数组样式括号 [] 语法来访问它:

document.inverse["inverse-cb1"].checked

或者您可以从 html 和您的文件中删除“-” JS。

I can't tell you what to do to make it "work", since your desired result is pretty unclear, but a number of problems in your code jumped out at me as I skimmed it top to bottom. Some will still run but give results you aren't expecting, but at least one problem will cause an error and stop the function running. So:

Most of your comments are either redundant and could be removed, or could be made redundant and removed if you made your variable names more descriptive.

The statement

if (isNaN(document.inverse.sin.value) == true || ...)

could be made simpler because you've already declared a variable sin that is equal to document.inverse.sin.value and the == true part is redundant. Just say

if (isNan(sin) || isNan(cos) || isNan(tan)) {

All of your "reset" statements like var sin = null; are both wrong and pointless because (a) using var means you are attempting to redeclare the variables (I think JS just ignores this), so you should just say sin = null;, but in any case (b) they're local variables within your trigI() function so they're all about to disappear anyway when the function returns. If you are trying to clear the fields on the screen you need to say document.inverse.sin.value = "";, but you don't want to annoy the user by clearing all the fields just because one of them was invalid. Tell them which field had a problem, set focus to that field, and let them correct it themselves.

Most of your if statement test something like this:

if (document.inverse-cb1.checked == 'false'){

Which will never be true because you are comparing the .checked property that is a boolean equal to true or false with the string 'false'. You need to say

if (document.inverse-cb1.checked == false){    // note: no quotes
// OR, even better
if (!document.inverse-cb1.checked){

What is sup.sup() supposed to do? sup is a variable that you've set to -1. sup() is a non-existent function that in any case you shouldn't be trying to call as a method of a number. Fix this first, because I think this is what is stopping the function returning.

EDIT: I noticed another big problem:

document.inverse.inverse-cb1.checked

You can't use the "dot" object notation for properties that don't follow the rules for JS identifier naming, and JS identifiers can't have a "-" in them. So although "inverse-cb1" is a valid property name you have to use the array-style bracket [] syntax to access it:

document.inverse["inverse-cb1"].checked

Or you could just remove the "-" from both the html and your JS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文