ianetaa
ianetaa

Reputation: 13

alert() window appears for every element in an array when looping?

I am trying to make some kind of search function, where a pop-up would appear and ask for an input from the customer and then compare it to the array items and return another alert window with either "found" or "not found"

Here is my code for the specific function:

this.searchItem = function(){
    var searchInput = window.prompt('Enter the search value:','');
    var i;
    for (i = 0; i< model.items.length; i++){
        if (model.items[i] == searchInput) {
            window.prompt("found");
        } else {
            window.prompt("not found");
        }
    }
}

It is kind of working. The problem is that it keeps showing a new alert window for every single element in the array. For example if I have 6 elements in the array and only one is matching the search input, then it will show me 5 alert windows with "not found" and one with "found". Another one appears after i close the previous one or if I click the ok button. How do I make it show me the alert window only once to tell me if it found it or not? Thanks!

Upvotes: 0

Views: 1252

Answers (6)

bakz
bakz

Reputation: 89

you are telling it to show the message every time it finds one, if you are seeking to finding only 1 match then just terminate the loop when you find it.

End the loop using break; or simply just set i to the arrays length:

......
 if (model.items[i] == searchInput) {
            window.prompt("found");
            return 0;
            }
.....
window.prompt("Not found"); //if fucntion doesn't return then it's not found.

OR

var found = false;
......
 if (model.items[i] == searchInput) {
            window.prompt("found");
            found = true;
            break;
            }
.....
if(!found) window.prompt("Not found");

OR

......
 if (model.items[i] == searchInput) {
            window.prompt("found");
            found = true;
            i = items.length;
            }
.....
if(!found) window.prompt("Not found");

Upvotes: 0

Cook Book
Cook Book

Reputation: 1

this.searchItem = function(){
    var searchInput = window.prompt('Enter the search value:','');
    var i;
    var found = false;
    for (i = 0; i< model.items.length; i++){
        if (model.items[i] == searchInput) {
           found = true;
        }
    }
    if(found == true)
        window.prompt("found");
    else
        window.prompt("not found")
}

Upvotes: 0

Matt Burland
Matt Burland

Reputation: 45155

Since you aren't doing anything else in your function after the loop, you can simply return when you find the element you were looking for. And only display the "not found" if you finish the loop without finding the element.

this.searchItem = function(){
    var searchInput = window.prompt('Enter the search value:','');
    var i;
    for (i = 0; i< model.items.length; i++){
        if (model.items[i] == searchInput) {
            window.alert("found");
            return;
        } 
    }
    window.alert("not found");
}

You could also simplify this by using Array.prototype.indexOf or Array.prototype.includes

Upvotes: 0

jake
jake

Reputation: 136

To show alert only once you have to execute the alert after loop ends, not inside loop.

Upvotes: 0

Nicholas Hirras
Nicholas Hirras

Reputation: 2596

Instead of showing the alert every time, set a variable such as var found=true. After loop terminates then show alert based on status of that var.

Upvotes: 0

Rob M.
Rob M.

Reputation: 36511

Put the alert (not prompt) after the loop. Also need to switch to using a variable to track whether or not the item was found:

this.searchItem = function(){
    var searchInput = window.prompt('Enter the search value:','');
    var i;
    var found = false;
    for (i = 0; i< model.items.length; i++){
        if (model.items[i] == searchInput) {
            found = true;
            break;
        }
    }
    window.alert(found ? 'found' : 'not found');
}

Upvotes: 3

Related Questions