-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpart2_6.html
More file actions
79 lines (74 loc) · 2.59 KB
/
Copy pathpart2_6.html
File metadata and controls
79 lines (74 loc) · 2.59 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Character Count in Text</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: grey;
color: white;
text-align: center;
padding: 20px;
}
textarea, input {
margin-top: 10px;
padding: 10px;
border-radius: 5px;
border: none;
}
button {
background-color: pink;
padding: 10px;
border-radius: 5px;
border: none;
cursor: pointer;
}
button:hover {
background-color: #ff69b4;
}
#result {
margin-top: 20px;
}
</style>
</head>
<body>
<h1>Character Frequency Analyzer</h1>
<form id="charCountForm">
<textarea id="textContent" rows="4" cols="50" placeholder="Type your content here..."></textarea><br>
<input type="text" id="charInput" maxlength="1" placeholder="Enter a single character">
<button type="submit">Analyze</button>
</form>
<div id="result"></div>
<a href="homework6.html" style="color: pink;">Back to Homework 6</a>
<script>
document.getElementById('charCountForm').addEventListener('submit', function(event) {
event.preventDefault();
const text = document.getElementById('textContent').value;
const char = document.getElementById('charInput').value;
countCharacter(text, char);
});
function countCharacter(text, char) {
if (char.length === 0) {
alert("Please enter a character to search.");
return;
}
const regex = new RegExp(char, "gi");
const matches = text.match(regex);
const count = matches ? matches.length : 0;
if (count > 0) {
document.getElementById('result').textContent = `The character "${char}" appears ${count} times in the text.`;
} else {
const message = `Search character "${char}" not found in the content you typed.`;
openNewWindow(message);
document.getElementById('result').textContent = '';
}
}
function openNewWindow(message) {
const newWindow = window.open("", "", "width=300,height=100");
newWindow.document.write(`<p>${message}</p><button onclick="window.close()">Close</button>`);
}
</script>
</body>
</html>