Search an Array
Searching an Array¶
Question
- Does the array contain a specific value?
- How do we know if an Array has a certain value?
Answer
- Sometimes we need to search through an array to see if it contains a certain value.
- We solve this with a loop!
Demo
- Demo: Searching Array
- unit05/demos/demo-searching-array.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
|
Line(s) | Purpose |
---|---|
11-17 | Declare the variables we'll need. |
20 | Initialize our Boolean variable to false |
23-25 | Create an array of numbers |
28 | Get the form element |
31 | Get the user entered value from the form |
34 | Get the output element |
37-41 | Check if the input is a number. If entry is not a number or an empty value, output an error message and stop the function |
44 | If we make it to this line, we have a numeric value, so we convert it. |
47-52 | Search the array for the entered value. We are looping through the array until we find the value. If we find a match, we set our Boolean variable to true and exit the loop. If we never find it, the variable remains false. |
56-63 | Check to see if we found a match and generate the appropriate output. |
66 | Display the output |
69 | Return false to prevent browser refresh |
The Steps in a Search¶
Step 1. A loop.¶
- We need a
for
loop that loops through the array.
1 2 3 4 |
|
Step 2. An if
statement in the loop¶
-
Inside the loop we will have to have an
if
statement that compares the element in the array with the value we're searching for. -
The
search
term is what we're looking for. -
The
numbers[index]
will be one element in the array from the first to the last.
1 2 3 4 |
|
Step 3. A boolean variable¶
-
Next, we need a boolean variable that is initially set to
false
. -
It has to start with
false
so if we don't find anything it will remain false.
1 2 3 |
|
Step 4. Set boolean to true in loop¶
-
If our search term is equal to the current element in the loop then we need to set the boolean to true.
-
It's important to realize that we don't do anything else here. We're just determine if our search term is in the array. We'll do something about it later.
1 2 |
|
Step 5. Break!¶
-
If we did find something then we want to break out of the loop.
-
This is important because we want to know where in the array we found the element. The
index
variable will now be set to the right location. If we don't break the index will always be at the final number in the array.
1 2 |
|
Step 6. Do something if we found it.¶
- If our search term was found we want to do something.
1 2 3 4 |
|
-
Here's where we do something with the results of the search. What we do will vary with the problem we're trying to solve.
-
Sometimes we want to do something only if we don't find our search term. We can do that like this.
1 2 3 4 |
|
Warning
All or nothing!
All of the above are necessary for a search to work. If you leave out even one step it will be very hard to get this to work.
Labs¶
Labs
- Lab08: Searching an Array
- unit05/labs/lab-08-array-search.html
Exercises¶
Exercises
- Exercise for Lab08
- unit05/exercises/exercises-08.html