-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10. String Manipulation.html
More file actions
75 lines (61 loc) · 2.02 KB
/
Copy path10. String Manipulation.html
File metadata and controls
75 lines (61 loc) · 2.02 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JS String Methods 101</title>
<style>
body {
font-family: system-ui;
max-width: 500px;
margin: 2rem auto;
line-height: 1.5;
}
.result-box {
background: #f4f4f9;
padding: 1rem;
border-radius: 8px;
margin-top: 1rem;
}
input {
padding: 8px;
width: 60%;
}
button {
padding: 8px 16px;
cursor: pointer;
}
b {
color: #2c3e50;
}
</style>
</head>
<body>
<h2>String Operations</h2>
<input id="userInput" type="text" placeholder="Type something...">
<button onclick="processString()">Run</button>
<div id="display" class="result-box">
Results will appear here...
</div>
<script>
function processString() {
const rawValue = document.getElementById("userInput").value;
const str = rawValue.trim(); // Removes whitespace from both ends
if (!str) {
document.getElementById("display").innerHTML = "<em>Please enter some text.</em>";
return;
}
// Using an array to build the output clearly
const results = [
`<b>Original (trimmed):</b> ${str}`,
`<b>.length:</b> ${str.length} (Total characters)`,
`<b>.toUpperCase():</b> ${str.toUpperCase()}`,
`<b>.toLowerCase():</b> ${str.toLowerCase()}`,
`<b>.charAt(0):</b> ${str.charAt(0)} (First letter)`,
`<b>.slice(0, 3):</b> ${str.slice(0, 3)} (First three letters)`,
`<b>Template Literal:</b> "Welcome, ${str}!"`
];
document.getElementById("display").innerHTML = results.join("<br>");
}
</script>
</body>
</html>