-
Notifications
You must be signed in to change notification settings - Fork 0
/
createBingSearchEngine.js
168 lines (155 loc) · 3.89 KB
/
createBingSearchEngine.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// COMPLETE SOLUTION
searchAlgorithm = (userInput, websiteList) => {
let searchResults = [];
// 1) userInput empty / undefined
if (userInput === "" || userInput === undefined) {
searchResults.push("No User Input.");
return searchResults;
// 2) websiteList undefined
} else if (websiteList === undefined) {
searchResults.push("Bing is currently offline. Please try google.com.");
return searchResults;
} else {
// remove spaces from userInput
if (/\s/.test(userInput)) {
userInput = userInput.replace(/\s/g, "");
}
userInput = userInput.toLowerCase();
console.log(userInput); // convert to lowercase
// 3) push matching search results to new array
for (var i = 0; i < websiteList.length; i++) {
if (websiteList[i].includes(userInput)) {
searchResults.push(websiteList[i]);
}
}
return searchResults;
}
};
//////////// TESTS
Test.assertDeepEquals(
searchAlgorithm("cat", [
"catphotos.com",
"jumanji.com",
"googlesucks.com",
"catcraycray.com",
]),
["catphotos.com", "catcraycray.com"]
);
Test.assertDeepEquals(
searchAlgorithm("eleven", [
"seveneleven.com",
"jumanji.com",
"eleventyfirstbirthday.com",
"catcraycray.com",
]),
["seveneleven.com", "eleventyfirstbirthday.com"]
);
Test.assertDeepEquals(
searchAlgorithm("jim", [
"jim.com",
"jumanjim.com",
"googlesucks.com",
"catcraycray.com",
]),
["jim.com", "jumanjim.com"]
);
Test.assertDeepEquals(
searchAlgorithm("", [
"seveneleven.com",
"jumanji.com",
"eleventyfirstbirthday.com",
"catcraycray.com",
]),
["No User Input."]
);
Test.assertDeepEquals(
searchAlgorithm(undefined, [
"seveneleven.com",
"jumanji.com",
"eleventyfirstbirthday.com",
"catcraycray.com",
]),
["No User Input."]
);
Test.assertDeepEquals(searchAlgorithm("dog", undefined), [
"Bing is currently offline. Please try google.com.",
]);
Test.assertDeepEquals(
searchAlgorithm("on", ["jon.com", "no.com", "yes.com", "gum.com"]),
["jon.com"]
);
Test.assertDeepEquals(
searchAlgorithm("every body dance now", [
"everybodydancenow.com",
"nobodydancenow.com",
"thecasperslideparttwo.com",
"itstimetogetfunky.com",
]),
["everybodydancenow.com"]
);
Test.assertDeepEquals(
searchAlgorithm("the casper slide part two", [
"everybodydancenow.com",
"nobodydancenow.com",
"thecasperslideparttwo.com",
"itstimetogetfunky.com",
]),
["thecasperslideparttwo.com"]
);
Test.assertDeepEquals(
searchAlgorithm("TO THE LEFT", [
"totheleft.com",
"takeitbacknowyall.com",
"onehopthistime.com",
"reversereverse.com",
]),
["totheleft.com"]
);
Test.assertDeepEquals(
searchAlgorithm("ChAr Lie BroWN", [
"totheleft.com",
"takeitbacknowyall.com",
"onehopthistime.com",
"reversereverse.com",
"charliebrown.com",
]),
["charliebrown.com"]
);
// RANDOM TESTS
makeString2Chars = () => {
var array = [];
var possible = "abdefghijklnpqrstuvwxyz"; // exclude 'c', 'o', 'm' so won't match .com
for (var i = 0; i < 2; i++) {
array.push(possible[Math.floor(Math.random() * possible.length)]);
}
return array.join("");
};
makeString20Chars = () => {
var array = [];
var possible = "abdefghijklnpqrstuvwxyz";
for (var i = 0; i < 20; i++) {
array.push(possible[Math.floor(Math.random() * possible.length)]);
}
return array.join("");
};
// run random test
randomTest = () => {
const randomString = makeString2Chars();
const websitesList = [];
for (var i = 0; i < 100; i++) {
websitesList.push(makeString20Chars() + ".com");
}
let expectedResult = [];
for (var i = 0; i < websitesList.length; i++) {
if (websitesList[i].includes(randomString)) {
expectedResult.push(websitesList[i]);
}
}
Test.assertDeepEquals(
searchAlgorithm(randomString, websitesList),
expectedResult
);
};
for (var i = 0; i < 100; i++) {
randomTest();
}