Reputation:
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=" ";
}
I was told that using break in the loop is a very bad programming technique. That the loop must be controlled by the condition, how do I go about fixing this? Thank you
Upvotes: 1
Views: 50
Reputation: 1439
I've personally seen some bad uses of break
in my career, but the break in this case isn't really necessary as it doesn't save you much, but it will save a few loop cycles on average.
Solution 1 is basically the same as your solution, but I would say it takes longer to reason about what is going on than your original solution. Many people blindly say not to use break
because they think it is just like a goto
in assembly. It is kind of like that, but when not abused it is perfectly fine.
Solution 1 doing the same thing but with an extra Boolean in the loop:
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++;
}
Solution 2 just leave out the break and look at everything:
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++;
}
Upvotes: 0
Reputation: 138557
document.getElementById("msg").innerHTML+= searchValue+((arrNums.indexOf(searchValue)+1)?" does":" does not")+" exist in the Array";
No need to use loops at all...
About breaks:
You should avoid infinite loops. Thats why people recommend to use a condition, because its much easier to overview, however, just breaking is fine if you know what youre doing...
Upvotes: 0
Reputation: 386883
You could use found
as exit variable in the while condition, like
while (!found && index < arrNums.length) {
// ^^^^^^^^^
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 = " ";
}
<input type="text" id="searchValue" onchange="start();">
<div id="msg"></div>
Upvotes: 1