-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
102 lines (96 loc) · 2.79 KB
/
script.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
var magazineWords,
word,
flag1 = 0,
notAvail = [];
const disp1 = document.getElementById("disp1");
const disp2 = document.getElementById("disp2");
//splitting the magazine text into separate words
function splitMagazine(str) {
if (!str) {
alert("Invalid!!! Don't leave the boxes empty!");
disp1.style.display = "none";
disp2.style.display = "none";
exit;
}
magazineWords = str.split(" ");
}
//splitting the input text into separate words
function splitWords(str) {
if (!str) {
alert("Invalid!!! Don't leave the boxes empty!");
disp1.style.display = "none";
disp2.style.display = "none";
exit;
}
word = str.split(" ");
}
function takeIn() {
var str = document.getElementById("magazine").value;
//replacing all the special characters with space
str = str.replace(
/[\!\@\#\$\%\^\&\*\)\(\+\=\.\<\>\{\}\[\]\:\;\'\"\|\~\`\_\-\\\/\,\?\n]/g,
" "
);
splitMagazine(str);
}
function callFunction() {
takeIn();
notAvail.splice(0, notAvail.length);
var str = document.getElementById("words").value;
str = str.replace(
/[\!\@\#\$\%\^\&\*\)\(\+\=\.\<\>\{\}\[\]\:\;\'\"\|\~\`\_\-\\\/\,\?\n]/g,
" "
);
str = str.replace(/\n/g, " ");
str = str.replace(" ", " ");
// console.log(str);
splitWords(str);
var i;
for (i = 0; i < word.length; i++) {
compareBoth(word[i]);
}
}
function compareBoth(wrd) {
var i,
flag = 0;
//comparing each word left by kidnapper with that in the magazine
for (i = 0; i < magazineWords.length; i++) {
if (magazineWords[i] !== null) {
if (magazineWords[i].localeCompare(wrd) == 0) {
flag = 1;
magazineWords[i] = null;
break;
}
}
}
if (flag != 1) {
notAvail.push(wrd); //pushing the unexisting words into notAvail
}
disp1.style.display = "none";
disp2.style.display = "none";
//displaying the final result
if (notAvail.length == 0) {
disp1.style.display = "block";
document.getElementById("demopos1").innerHTML =
"All the words exist in this Magazine.";
document.getElementById("demopos2").innerHTML =
"Yes. The Kidnapper has taken all the ransom words from this Magazine only.";
} else {
disp2.style.display = "block";
if (notAvail.length == 1) {
document.getElementById("demoneg1").innerHTML =
notAvail.length +
' word "' +
notAvail +
'" does not exist in this Magazine.';
} else {
document.getElementById("demoneg1").innerHTML =
notAvail.length +
' words "' +
notAvail +
'" do not exist in this Magazine.';
}
document.getElementById("demoneg2").innerHTML =
"No. The Kidnapper has not taken the words from this Magazine.";
}
}