forked from MrTheiss/01_IntroToJS
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmain.js
More file actions
61 lines (47 loc) · 1.31 KB
/
main.js
File metadata and controls
61 lines (47 loc) · 1.31 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
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
// Create globals for Exit Code and Fruit Array
var _FRUITS = [];
var _EXIT_CODE = 999;
// This function sets variables to certain values
function InitializeVariables() {
_FRUITS = ['Apple', 'Banana', 'Cherry'];
}
/*
// IsFruitInList
//
// Returns bool whether or not the fruit is in a list
*/
function IsFruitInList(fruitToCheck, fruitList) {
// Create a flag for whether or not the flag is found
var foundFruitInList = false;
// Loop through the fruitList and check if the fruit exists
for (var i = 0; i < fruitList.length; i++) {
// The one condition is if the fruit matches a fruit in the fruitList
if (fruitToCheck == fruitList[i]) {
foundFruitInList = true;
}
}
// Return wether or not we found the fruit in the list
return foundFruitInList;
}
/*
// CheckFruitsFromUserInput
//
// Function checks wether a fruit is in GLOBAL _FRUITS list
*/
function CheckFruitsFromUserInput() {
var fruitToCheck;
while (true) {
// Input from user
var fruitToCheck = prompt("Please enter a fruit to check. (type " + _EXIT_CODE + " to quit)", "");
// See if we need to exit the loop
if (fruitToCheck == _EXIT_CODE) {
return;
}
// Check if the fruit is in the fruit list
if (IsFruitInList(fruitToCheck, _FRUITS)) {
alert('You guessed one of the fruits!');
} else {
alert('NOPE');
}
}
}