Northcentral Technical College Introduction To Programming
10-152-310
Introduction to Programming with JavaScript NTC 10-152-310

Learning Plan 6 Searching an Array

# Searching an Array

#### File: /Learning Plan 6/searchingAnArray.html

- Does the array contain a specific value?

### How do we know if an Array has a certain value?

- Sometimes we need to search through an array to see if it contains a certain value.
- We solve this with a loop!

Number:

- Lines 3-9: Declare the variables we'll need.
- Lines 11-13: Create the array and populate it.
- Lines 15-17: Get the entered value from the form.
- Line 19: Get the output HTML element.
- Lines 21-24: Checking for a number! Don't forget. Because we're in a function, we can output the message and `return false;`. Returning false means that the browser won't refresh the page.
- Line 27: Convert to a number.
- Lines 29-34: Here's the search. We just have to loop through the array until we find the value. If we do then we set a boolean variable to `true`. If we never find it then the variable remains `false`.
- Lines 26-44 If we found the entry in the array then set the output. If not then set a message.
- Line 47: Send the output to the HTML display element.
- Line 49: Returning false will prevent the browser from refreshing the page.

### The Steps in a Search

#### Step 1. A loop.

- We need a `for` loop that loops through the array.

#### 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.

#### Step 3. A boolean variable

- Next, we need a boolean variable that is set to `false`.
- It has to start with `false` so if we don't find anything it will remain false.

#### 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.

#### 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 we found the element. The `index` variable will now be set to the right number. If we don't break the index will always be at the final number in the array.

#### Step 6. Do something if we found it.

- If our search term was found we want to do something.
- 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.

#### 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.

### Got it? Good, here's a lab.

- 6.2 Lab 2
  - 6.2 Assignment 2