当文本框在最多 3 个数字算法中为空时发出特定于 JavaScript 的警报



我是编码新手,并尝试使用javascript解决算法。我必须在 3 个文本框中输入 3 个数字中找到最大值并提醒它。我已经这样做了,但是当文本框留空且没有输入时,我仍然必须提醒一些消息。因此,当所有 3 个字段都为空时,我必须生成一条特定的警报消息,尽管我写了一些东西,但我总是得到:"NaN"警报。我想要的不是NaN,而是"Introduceti cel putin un numar"。你能帮我这个吗?这是我的代码(html和javascript(。

谢谢!

<!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>

和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);
    }
}

当您将空值转换为 int 时,它将是 NaN(而不是数字(,因此您需要使用 isNaN(value( 处理这种情况。 如果 isNaN 的值不是其他明智的数字 false,则返回 true。

在 parseInt(( 步骤之后,如果输入值为空,a的值将为 NaN。 现在我在你的 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>

注1:&&的优先级高于||,则应将测试括在括号||

注意2:您只需检查tha值的length即可检查它们是否为空。

注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);
    }
}

最新更新