简体   繁体   English

当文本框最多为3个数字时,JavaScript特定警报为空

[英]javascript specific alert when textbox is empty in maximum of 3 numbers algorithm

I'm new to coding and trying to solve an algorithm using javascript. 我是编码和尝试使用javascript解决算法的新手。 I have to find the maximum out of 3 numbers input in 3 textboxes and alert it. 我必须在3个文本框中找到输入的3个数字中的最大值,并对其进行提醒。 I have done that but I still have to alert some messages when textboxes are left empty, without inputs. 我已经做到了,但是当文本框留空而没有输入时,我仍然必须提醒一些消息。 So when all 3 fields remain empty I have to generate a specific alert message and although I wrote something, I always get: "NaN" alert. 因此,当所有3个字段都保留为空时,我必须生成特定的警报消息,尽管我写了一些东西,但总会得到:“ NaN”警报。 Instead of NaN I want to get: "Introduceti cel putin un numar". 而不是NaN,我想得到:“ Introduceti cel putin un numar”。 Can you help me with this? 你能帮我吗? Here is my code (html and javascript). 这是我的代码(html和javascript)。

Thanks! 谢谢!

<!doctype HTML>
<html>

<head>
    <title>Algoritmi</title>
    <script src="point2.js">

    </script>
</head>

<body>
     <form name="mainForm" style="text-align:center">
        <input type="text" name="textBox1" /> <br/>
        <input type="text" name="textBox2" /> <br/>
        <input type="text" name="textBox3" /> <br/>
        <input type="button" value="Afiseaza maxim" onclick=" javascript: calcMax()" />
    </form>
</body>

</html>

And Javascript: 和Javascript:

function calcMax() {
    var a = parseInt(document.mainForm.textBox1.value);
    var b = parseInt(document.mainForm.textBox2.value);
    var c = parseInt(document.mainForm.textBox3.value);
    var max;
    if (a == null || a == "" && b == null || b == "" && c == null || c == "") {
        alert("Introduceti cel putin un numar");
    }
    else {
        if (a > b) {
            if (a > c) {
                max = a
            }
            else {
                max = c;
            }
        }
        else {
            if (b > c) {
                max = b
            }
            else {
                max = c;
            }
        }
        alert(max);
    }
}

when you convert empty value to int it will be NaN(not a number) so you need to handle this condition using isNaN (value). 当您将空值转换为int时,它将为NaN(不是数字),因此您需要使用isNaN (值)来处理此条件。 isNaN returns true if it's value is not a number other wise false. 如果isNaN的值不是一个数字,否则返回true;否则为false。

After parseInt() step the value of a will be NaN if the input value is empty. 在parseInt()步骤之后,如果输入值为空,则a的值将为NaN。 now i added isNaN(a) condition in your if condition 现在我在您的if条件中添加了isNaN(a)条件

 function calcMax() { var a = parseInt(document.mainForm.textBox1.value); var b = parseInt(document.mainForm.textBox2.value); var c = parseInt(document.mainForm.textBox3.value); var max; if ((a == null || a == "" || isNaN(a) ) && (b == null || b == "" || isNaN(b)) && (c == null || c == "" || isNaN(c))) { alert("Introduceti cel putin un numar"); } else { if (a > b) { if (a > c) { max = a } else { max = c; } } else { if (b > c) { max = b } else { max = c; } } alert(max); } } 
 <!doctype HTML> <html> <head> <title>Algoritmi</title> <script src="point2.js"> </script> </head> <body> <form name="mainForm" style="text-align:center"> <input type="text" name="textBox1" /> <br/> <input type="text" name="textBox2" /> <br/> <input type="text" name="textBox3" /> <br/> <input type="button" value="Afiseaza maxim" onclick=" javascript: calcMax()" /> </form> </body> </html> 

Note 1: && has higher precedence than || 注1: &&优先级高于|| , you should wrap || ,您应该将||换行 test in parenthesis. 在括号中进行测试。

Note 2: You can just check the length of tha values to check if they are empty or not. 注意2:您可以检查tha值的length以检查它们是否为空。

Note 3: To calculate the max of the three value, you can simply use Math.max . 注意3:要计算三个值的最大值,您可以简单地使用Math.max

function calcMax() {
    // no need for parseInt
    var a = document.mainForm.textBox1.value;               // you can add .trim() at the end to remove surrounding spaces
    var b = document.mainForm.textBox2.value;               // ...
    var c = document.mainForm.textBox3.value;               // ...
    var max;

    if (a.length * b.length * c.length === 0) {             // at least one of the inputs value's length is 0
        alert("You must fill the inputs");
    }
    else if(isNaN(a) || isNaN(b) || isNaN(c)) {             // at least one of the inputs value is not a valid number
        alert("inputs must be filled with numbers");
    }
    else {
        max = Math.max(a, b, c);                            // a, b and c are all numbers, use Math.max to calculate the maximum (at this stage a, b and c are string, Math.max will internally convert them to numbers)
        alert(max);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM