-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
104 lines (87 loc) · 2.42 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Words Count</title>
<style>
div{
background-color: rgb(72, 184, 249);
margin: 0 500px;
height: 650px;
width: 650px;
border-radius: 25px;
}
h1{
text-align: center;
text-transform: capitalize;
font-size: 50px;
}
label{
margin: 0 255px;
font-size: 25px;
}
input{
margin: 0 60px;
height: 100px;
width: 500px;
border-radius: 20px;
}
textarea{
margin: 0 60px;
height: 250px;
width: 500px;
border-radius: 25px;
}
button{
margin: 0 200px;
width: 250px;
height: 30px;
border-radius: 75px;
background-color: rgb(103, 5, 250);
color: aliceblue;
}
</style>
</head>
<body>
<div id="root">
<u><h1>Count the words</h1></u>
<u><label> Input words</label></u>
<br/>
<br/>
<input id="para" type="string" placeholder="Enter the words"></input>
<br/>
<br/>
<button onclick="countWords()">count</button>
<br/>
<br/>
<br/>
<u><label> Words count:</label></u>
<br/>
<br/>
<textarea id="output" placeholder="Total Words Const"></textarea>
</div>
<script>
function countWords() {
// get the value in input box
let text = document.getElementById('para').value;
//remove extra
text = text.replace(/[.,]/g, "");
text = text.toUpperCase();
const wordArray = text.split(" ");
console.log(wordArray);
const wordCount = {};
wordArray.forEach((item) => {
if (wordCount[item] == null) wordCount[item] = 1;
else {
wordCount[item] += 1;
}
});
let myArray = Object.entries(wordCount);
let bArray = myArray.sort(function (a, b) { return [b[1] - a[1]] });
bArray = bArray.slice(0, myArray.length);
return document.getElementById('output').innerHTML = bArray;
}
</script>
</body>
</html>