-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountWord.js
93 lines (65 loc) · 1.89 KB
/
CountWord.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
var CountWord = function(){
var startButton = document.getElementById("start");
var string = document.getElementById("string");
var word = document.getElementById("word");
var test = document.getElementById("testWord");
startButton.onclick = startCount;
}
;
var startCount = function(){
var output = document.getElementById("result");
var currentParagraph = string.value;
var theWord = word.value;
if (currentParagraph.length > 0)
{
if (theWord.length > currentParagraph.length)
{
output.innerHTML = "Invalid Word! Please put a word that is shorter then the paragraph..." ;
}
else
{
if (theWord.length === 0)
{
output.innerHTML = "Pleae input a word first..."
}
else
{
output.innerHTML = theWord + " appeared " + Count(currentParagraph,theWord) + " times" ;
};
}
}
else
{
output.innerHTML = "Please input a paragraph first...";
};
return false;
};
var Count = function(str,wd){
var lenWd = wd.length;
var lenStr = str.length;
var currentLetterinStr = 0;
var currentLetterinWd = 0;
var numberOftheSameLetter = 0;
var numberOfPresence = 0;
var i = 0;
for (i = 0; i < lenStr - lenWd + 1; i++)
{
for (currentLetterinWd = 0; currentLetterinWd <lenWd; currentLetterinWd++)
{
// alert(wd[currentLetterinWd]);
if(str[i+ currentLetterinStr] === wd[currentLetterinWd])
{
numberOftheSameLetter++;
}
currentLetterinStr++;
}
if (numberOftheSameLetter === lenWd)
{
numberOfPresence++;
}
numberOftheSameLetter = 0;
currentLetterinStr = 0;
};
return numberOfPresence;
};
window.onload = CountWord;