-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.js
More file actions
29 lines (24 loc) · 1.25 KB
/
search.js
File metadata and controls
29 lines (24 loc) · 1.25 KB
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
/* Important note
The behavior of most browsers has changed since this video was shot, so you won't see the same thing as I demonstrate in the video. In the video, you'll see that my script is able to print out to the browser using document.write( ) while inside a loop.
Most browsers no longer do that: they wait until the loop finishes and then they print to the window. So, you'll see a blank page until you type quit in the prompt window — then you'll see all the output printed to the screen.
Sorry for the confusion, and we'll update the video soon. */
var inStock = [ 'apples', 'eggs', 'milk', 'cookies', 'cheese', 'bread', 'lettuce', 'carrot', 'broccoli', 'pizza', 'potato', 'crackers', 'onion', 'tofu', 'frozen dinner', 'cucumber'];
var search;
function print(message) {
document.write( '<p>' + message + '</p>');
}
while (true) {
search = prompt("search for the product in our store. Type 'list' to show all of the produce and 'quit' to exit");
search = search.toLowerCase();
if (search === 'quit') {
break;
} else if (search === "list") {
print(inStock.join(", "));
} else {
if (inStock.indexOf(search) > -1) {
print("yes, we have " + search + " in the store.");
} else {
print (search + " is not in stock." );
}
}
}