-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolve.js
More file actions
82 lines (73 loc) · 1.88 KB
/
solve.js
File metadata and controls
82 lines (73 loc) · 1.88 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var total;
var map1;
let str = [];
var set2;
var wrd = "";
var out = [];
function clear_all() {
map1 = new Map();
total = "";
out.length = 0;
wrd = "";
document.getElementById("demo").innerHTML = total;
document.getElementById("maps").innerHTML = out;
}
function search_input() {
total = document.getElementById("txt").value;
total = total.toLowerCase();
var temp = total;
document.getElementById("demo").innerHTML = total;
map1 = new Map();
var set1 = new Set();
str = total.split('\n\n');
for(var i = 0; i < str.length; i++) {
var para = str[i]; // is a paragraph
var words = para.split(" "); // words
// i is the paragraph number
for(var j = 0; j < words.length; j++) {
words[j] = words[j].replace(/[&\/\\#,+()$~%.'":*?<>{}\n]/g, '');
var key = words[j];
map1[key] = map1[key] || [];
map1[key].push(i+1);
set1.add(words[j]);
}
}
// DEBUG FROM HERE
// for (let item of set1) {
// console.log(item);
// for(let it of map1[item]){
// console.log(it);
// }
// }
}
function search_output() {
// wrd is word to search in top 10
wrd = document.getElementById("text1").value;
wrd = wrd.toLowerCase();
var topTen = 0;
set2 = new Set();
out.length = 0;
// no text
if( (total.length == 0) || (total == undefined) ) {
document.getElementById("maps").innerHTML = "Enter Text please"
}
// present
else if(map1[wrd] != undefined) {
for(let it of map1[wrd]) {
set2.add(it);
}
for(let item of set2) {
if(topTen == 10) {
break;
}
out.push(item);
topTen = topTen + 1;
}
document.getElementById("maps").innerHTML = out;
}
// word not found
else {
alert("word not found, please try another");
document.getElementById("maps").innerHTML = "oops!! Word Not Found , Please try another !";
}
}