不允许使用break,因为它是一种糟糕的编程技术

时间:2017-05-17 16:49:52

标签: javascript

function start() {
    var arrNums = [18,23,20,17,21,18,22,19,18,20];
    var searchValue, index, found;
    found = false;
    index = 0;
    searchValue = document.getElementById("searchValue").value;
    document.getElementById("msg").innerHTML+="The values in the array are: ";
    while(index < arrNums.length) {
        document.getElementById("msg").innerHTML+= arrNums[index] + " ";
        index++;
    }

    index = 0;

    while (index < arrNums.length) {
        if (arrNums[index] == searchValue) {
            document.getElementById("msg").innerHTML+= "<br/> The number " + searchValue + " exists in the array";
            document.getElementById("msg").innerHTML+= "<br/> The values in this example do match the vaules you must use.";
            found = true;
            break;
        }
        index++;
    }       

    if (!found) {       
        document.getElementById("msg").innerHTML+= "<br/> The number " + searchValue + " does not exist in the array";
        document.getElementById("msg").innerHTML+= "<br/> The values in this example do not match the vaules you must use.";
    }

}

function clearOutput() {
    document.getElementById("msg").innerHTML=" ";
}  

我被告知在循环中使用break是一种非常糟糕的编程技术。循环必须由条件控制,我该如何解决这个问题呢? 谢谢

3 个答案:

答案 0 :(得分:1)

您可以在while条件中使用found作为退出变量,例如

while (!found && index < arrNums.length) {
//     ^^^^^^^^^

&#13;
&#13;
function start() {
    var arrNums = [18, 23, 20, 17, 21, 18, 22, 19, 18, 20];
    var searchValue, index, found;
    found = false;
    index = 0;
    searchValue = document.getElementById("searchValue").value;
    document.getElementById("msg").innerHTML += "The values in the array are: ";
    while (index < arrNums.length) {
        document.getElementById("msg").innerHTML += arrNums[index] + " ";
        index++;
    }

    index = 0;

    while (!found && index < arrNums.length) {
        if (arrNums[index] == searchValue) {
            document.getElementById("msg").innerHTML += "<br/> The number " + searchValue + " exists in the array";
            document.getElementById("msg").innerHTML += "<br/> The values in this example do match the vaules you must use.";
            found = true;
        }
        index++;
    }

    if (!found) {
        document.getElementById("msg").innerHTML += "<br/> The number " + searchValue + " does not exist in the array";
        document.getElementById("msg").innerHTML += "<br/> The values in this example do not match the vaules you must use.";
    }
}

function clearOutput() {
    document.getElementById("msg").innerHTML = " ";
}
&#13;
<input type="text" id="searchValue" onchange="start();">
<div id="msg"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

document.getElementById("msg").innerHTML+= searchValue+((arrNums.indexOf(searchValue)+1)?" does":" does not")+" exist in the Array";

根本不需要使用循环......

关于休息时间:

你应该避免无限循环。这就是为什么人们建议使用条件,因为它更容易概述,但是,如果你知道你正在做什么,只是打破就好了...

答案 2 :(得分:0)

我个人在我的职业生涯中看到了break的一些不良用途,但在这种情况下的突破并不是真的有必要,因为它并没有为你节省太多,但它会平均节省几个循环周期。

解决方案1与您的解决方案基本相同,但我会说,与原始解决方案相比,需要更长的时间来推断正在发生的事情。许多人盲目地说不使用break,因为他们认为它就像汇编中的goto一样。它有点像,但是如果没有被滥用,它就完全没问题了。

解决方案1做同样的事情,但在循环中使用额外的布尔值:

while (index < arrNums.length and !found) {
    if (arrNums[index] == searchValue) {
        document.getElementById("msg").innerHTML+= "<br/> The number " + searchValue + " exists in the array";
        document.getElementById("msg").innerHTML+= "<br/> The values in this example do match the vaules you must use.";
        found = true;
    }
    index++;
}

解决方案2只需忽略休息并查看所有内容:

while (index < arrNums.length) {
    if (arrNums[index] == searchValue) {
        document.getElementById("msg").innerHTML+= "<br/> The number " + searchValue + " exists in the array";
        document.getElementById("msg").innerHTML+= "<br/> The values in this example do match the vaules you must use.";
        found = true;
    }
    index++;
}