0% found this document useful (0 votes)
13 views2 pages

Experiment 2

The document provides an HTML template for a Palindrome Checker using JavaScript. It includes a function that accepts user input, cleans the string, reverses it, and checks if it is a palindrome. The result is displayed on the webpage based on the comparison of the cleaned and reversed strings.

Uploaded by

nopekevin075
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Experiment 2

The document provides an HTML template for a Palindrome Checker using JavaScript. It includes a function that accepts user input, cleans the string, reverses it, and checks if it is a palindrome. The result is displayed on the webpage based on the comparison of the cleaned and reversed strings.

Uploaded by

nopekevin075
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Create event driven JavaScript program for the following.

Make use of appropriate


variables, JavaScript inbuilt string functions and control structures.

To accept string from user and reverse the given string and check whether it is
palindrome or not.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Palindrome Checker</title>
<script>
// Function to check if the string is a palindrome
function checkPalindrome() {
// Get the input string from the user
let inputString = document.getElementById("userInput").value;

// Remove any non-alphanumeric characters and convert to lowercase


let cleanedString = inputString.replace(/[^A-Za-z0-9]/g, '').toLowerCase();

// Reverse the cleaned string


let reversedString = cleanedString.split('').reverse().join('');

// Check if the original string and reversed string are the same
if (cleanedString === reversedString) {
document.getElementById("result").innerText = "The string is a
palindrome!";
} else {
document.getElementById("result").innerText = "The string is not a
palindrome.";
}
}
</script>
</head>
<body>
<h2>Palindrome Checker</h2>

<label for="userInput">Enter a string:</label>


<input type="text" id="userInput" placeholder="Enter a string">
<button onclick="checkPalindrome()">Check Palindrome</button>

<p id="result"></p>
</body>
</html>
Output :-

You might also like