<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Fun With Text</title>
<!-- Google Font -->
<link
href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,400;0,500;1,500&display=swap"
rel="stylesheet"
/>
<!-- Stylesheet -->
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
<textarea
rows="3"
placeholder="Type something here.."
id="my-text-box"
></textarea>
<div id="result">Press Any Of The Buttons Below To Get Started</div>
<div class="buttons">
<button onclick="reverseStr()">Reverse</button>
<button onclick="isPalindrome()">Is Palindrome</button>
</div>
<div class="buttons">
<button onclick="charCount()">Character Count</button>
<button onclick="wordCount()">Word Count</button>
</div>
<div class="search-container">
<input
type="text"
id="search-text"
placeholder="Type the word to be searched"
/>
<button onclick="search()">Search</button>
</div>
</div>
<!-- Script -->
<script src="script.js"></script>
</body>
</html>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}
body {
height: 100vh;
background: linear-gradient(#fafbff 50%, #4581f6 50%);
}
.container {
position: absolute;
transform: translate(-50%, -50%);
left: 50%;
top: 50%;
background-color: #ffffff;
width: 80vmin;
padding: 3em 1.8em;
box-shadow: 0 1.25em 3em rgba(0, 0, 0, 0.18);
border-radius: 0.5em;
}
textarea {
resize: none;
width: 100%;
font-size: 1.1em;
padding: 0.5em;
border: 1px solid #000c27;
border-radius: 0.2em;
}
#result {
background-color: #dce7ff;
text-align: center;
font-size: 1.1em;
padding: 1.35em 0.5em;
margin: 0.8em 0 1.6em 0;
color: #000c27;
}
#result span {
font-weight: 500;
font-style: italic;
}
.buttons {
display: flex;
justify-content: space-between;
gap: 0.3em;
width: 80%;
margin: 0.6em auto;
}
.buttons button {
background-color: #4581f6;
font-size: 1em;
border: none;
width: 48%;
padding: 0.8em 0;
color: #ffffff;
border-radius: 0.2em;
}
.search-container {
width: 80%;
margin: 1.8em auto 0 auto;
display: grid;
grid-template-columns: 9fr 3fr;
gap: 0.6em;
}
.search-container input {
font-size: 1em;
padding: 0.7em 0.3em;
border-radius: 0.3em;
border: 1px solid #000c27;
}
.search-container button {
font-size: 1em;
background-color: #4581f6;
color: #ffffff;
border: none;
border-radius: 0.3em;
}
@media screen and (max-width: 37.5em) {
.buttons {
width: 100%;
}
.search-container {
width: 100%;
}
}
let myTextBox = document.getElementById("my-text-box");
let result = document.getElementById("result");
//Validation for empty field
let isEmpty = () => {
if (myTextBox.value.length != 0) {
return false;
} else {
return true;
}
};
//Function to reverse text
let reverseStr = () => {
if (isEmpty()) {
result.innerHTML = "Please Enter Some Text";
} else {
let myText = myTextBox.value;
result.innerHTML = `The reversed text is: <span>${myText
.split("")
.reverse()
.join("")}</span>`;
}
};
//Function to check palindrome
let isPalindrome = () => {
if (isEmpty()) {
result.innerHTML = "Please Enter Some Text";
} else {
let myText = myTextBox.value.replace(/[^a-zA-Z0-9]/g, "").toLowerCase();
if (myText == myText.split("").reverse().join("")) {
result.innerHTML = `It is <span>A Palindrome</span>`;
} else {
result.innerHTML = `It is <span>Not A Palindrome</span>`;
}
}
};
//Count number of characters
let charCount = () => {
if (isEmpty()) {
result.innerHTML = "Please Enter Some Text";
} else {
let myText = myTextBox.value;
result.innerHTML = `The character count is : <span>${myText.length}</span>`;
}
};
//Count number of words
let wordCount = () => {
if (isEmpty()) {
result.innerHTML = "Please Enter Some Text";
} else {
let myText = myTextBox.value;
result.innerHTML = `The word count is: <span>${
myText
.trim()
.split(/\s+/)
.filter((item) => item).length
}</span>`;
}
};
//Search given word in the text
let search = () => {
let searchText = document.getElementById("search-text").value;
if (isEmpty() || searchText.length == 0) {
result.innerHTML = "Either Or Both Input Fields Are Empty";
} else {
let myText = myTextBox.value;
if (myText.includes(searchText)) {
result.innerHTML = `The text contains <span>'${searchText}'</span>`;
} else {
result.innerHTML = `The text does NOT contains <span>'${searchText}'</span>`;
}
}
};
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.