-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathbook_search.js
104 lines (94 loc) · 3.27 KB
/
book_search.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
/**
* RECOMMENDATION
*
* To test your code, you should open "tester.html" in a web browser.
* You can then use the "Developer Tools" to see the JavaScript console.
* There, you will see the results unit test execution. You are welcome
* to run the code any way you like, but this is similar to how we will
* run your code submission.
*
* The Developer Tools in Chrome are available under the "..." menu,
* futher hidden under the option "More Tools." In Firefox, they are
* under the hamburger (three horizontal lines), also hidden under "More Tools."
*/
/**
* Searches for matches in scanned text.
* @param {string} searchTerm - The word or term we're searching for.
* @param {JSON} scannedTextObj - A JSON object representing the scanned text.
* @returns {JSON} - Search results.
* */
function findSearchTermInBooks(searchTerm, scannedTextObj) {
/** You will need to implement your search and
* return the appropriate object here. */
var result = {
"SearchTerm": "",
"Results": []
};
return result;
}
/** Example input object. */
const twentyLeaguesIn = [
{
"Title": "Twenty Thousand Leagues Under the Sea",
"ISBN": "9780000528531",
"Content": [
{
"Page": 31,
"Line": 8,
"Text": "now simply went on by her own momentum. The dark-"
},
{
"Page": 31,
"Line": 9,
"Text": "ness was then profound; and however good the Canadian\'s"
},
{
"Page": 31,
"Line": 10,
"Text": "eyes were, I asked myself how he had managed to see, and"
}
]
}
]
/** Example output object */
const twentyLeaguesOut = {
"SearchTerm": "the",
"Results": [
{
"ISBN": "9780000528531",
"Page": 31,
"Line": 9
}
]
}
/*
_ _ _ _ ___ _____ _____ _____ ____ _____ ____
| | | | \ | |_ _|_ _| |_ _| ____/ ___|_ _/ ___|
| | | | \| || | | | | | | _| \___ \ | | \___ \
| |_| | |\ || | | | | | | |___ ___) || | ___) |
\___/|_| \_|___| |_| |_| |_____|____/ |_| |____/
*/
/* We have provided two unit tests. They're really just `if` statements that
* output to the console. We've provided two tests as examples, and
* they should pass with a correct implementation of `findSearchTermInBooks`.
*
* Please add your unit tests below.
* */
/** We can check that, given a known input, we get a known output. */
const test1result = findSearchTermInBooks("the", twentyLeaguesIn);
if (JSON.stringify(twentyLeaguesOut) === JSON.stringify(test1result)) {
console.log("PASS: Test 1");
} else {
console.log("FAIL: Test 1");
console.log("Expected:", twentyLeaguesOut);
console.log("Received:", test1result);
}
/** We could choose to check that we get the right number of results. */
const test2result = findSearchTermInBooks("the", twentyLeaguesIn);
if (test2result.Results.length == 1) {
console.log("PASS: Test 2");
} else {
console.log("FAIL: Test 2");
console.log("Expected:", twentyLeaguesOut.Results.length);
console.log("Received:", test2result.Results.length);
}